Class Phalcon\Mvc\Model\Query\Builder

implements Phalcon\Mvc\Model\Query\BuilderInterface, Phalcon\Di\InjectionAwareInterface

Source on GitHub

Helps to create PHQL queries using an OO interface

  1. <?php
  2. $params = [
  3. "models" => ["Users"],
  4. "columns" => ["id", "name", "status"],
  5. "conditions" => [
  6. [
  7. "created > :min: AND created < :max:",
  8. [
  9. "min" => "2013-01-01",
  10. "max" => "2014-01-01",
  11. ],
  12. [
  13. "min" => PDO::PARAM_STR,
  14. "max" => PDO::PARAM_STR,
  15. ],
  16. ],
  17. ],
  18. // or "conditions" => "created > '2013-01-01' AND created < '2014-01-01'",
  19. "group" => ["id", "name"],
  20. "having" => "name = 'Kamil'",
  21. "order" => ["name", "id"],
  22. "limit" => 20,
  23. "offset" => 20,
  24. // or "limit" => [20, 20],
  25. ];
  26. $queryBuilder = new \Phalcon\Mvc\Model\Query\Builder($params);

Constants

string OPERATOR_OR

string OPERATOR_AND

Methods

public __construct ([mixed $params], [Phalcon\DiInterface $dependencyInjector])

Phalcon\Mvc\Model\Query\Builder constructor

public setDI (Phalcon\DiInterface $dependencyInjector)

Sets the DependencyInjector container

public getDI ()

Returns the DependencyInjector container

public distinct (mixed $distinct)

Sets SELECT DISTINCT / SELECT ALL flag

  1. <?php
  2. $builder->distinct("status");
  3. $builder->distinct(null);

public getDistinct ()

Returns SELECT DISTINCT / SELECT ALL flag

public columns (mixed $columns)

Sets the columns to be queried

  1. <?php
  2. $builder->columns("id, name");
  3. $builder->columns(
  4. [
  5. "id",
  6. "name",
  7. ]
  8. );
  9. $builder->columns(
  10. [
  11. "name",
  12. "number" => "COUNT(*)",
  13. ]
  14. );

public string | array getColumns ()

Return the columns to be queried

public from (mixed $models)

Sets the models who makes part of the query

  1. <?php
  2. $builder->from("Robots");
  3. $builder->from(
  4. [
  5. "Robots",
  6. "RobotsParts",
  7. ]
  8. );
  9. $builder->from(
  10. [
  11. "r" => "Robots",
  12. "rp" => "RobotsParts",
  13. ]
  14. );

public addFrom (mixed $model, [mixed $alias], [mixed $with])

Add a model to take part of the query

  1. <?php
  2. // Load data from models Robots
  3. $builder->addFrom("Robots");
  4. // Load data from model 'Robots' using 'r' as alias in PHQL
  5. $builder->addFrom("Robots", "r");
  6. // Load data from model 'Robots' using 'r' as alias in PHQL
  7. // and eager load model 'RobotsParts'
  8. $builder->addFrom("Robots", "r", "RobotsParts");
  9. // Load data from model 'Robots' using 'r' as alias in PHQL
  10. // and eager load models 'RobotsParts' and 'Parts'
  11. $builder->addFrom(
  12. "Robots",
  13. "r",
  14. [
  15. "RobotsParts",
  16. "Parts",
  17. ]
  18. );

public string | array getFrom ()

Return the models who makes part of the query

public Phalcon\Mvc\Model\Query\Builder join (string $model, [string $conditions], [string $alias], [string $type])

Adds an INNER join to the query

  1. <?php
  2. // Inner Join model 'Robots' with automatic conditions and alias
  3. $builder->join("Robots");
  4. // Inner Join model 'Robots' specifying conditions
  5. $builder->join("Robots", "Robots.id = RobotsParts.robots_id");
  6. // Inner Join model 'Robots' specifying conditions and alias
  7. $builder->join("Robots", "r.id = RobotsParts.robots_id", "r");
  8. // Left Join model 'Robots' specifying conditions, alias and type of join
  9. $builder->join("Robots", "r.id = RobotsParts.robots_id", "r", "LEFT");

public Phalcon\Mvc\Model\Query\Builder innerJoin (string $model, [string $conditions], [string $alias])

Adds an INNER join to the query

  1. <?php
  2. // Inner Join model 'Robots' with automatic conditions and alias
  3. $builder->innerJoin("Robots");
  4. // Inner Join model 'Robots' specifying conditions
  5. $builder->innerJoin("Robots", "Robots.id = RobotsParts.robots_id");
  6. // Inner Join model 'Robots' specifying conditions and alias
  7. $builder->innerJoin("Robots", "r.id = RobotsParts.robots_id", "r");

public Phalcon\Mvc\Model\Query\Builder leftJoin (string $model, [string $conditions], [string $alias])

Adds a LEFT join to the query

  1. <?php
  2. $builder->leftJoin("Robots", "r.id = RobotsParts.robots_id", "r");

public Phalcon\Mvc\Model\Query\Builder rightJoin (string $model, [string $conditions], [string $alias])

Adds a RIGHT join to the query

  1. <?php
  2. $builder->rightJoin("Robots", "r.id = RobotsParts.robots_id", "r");

public array getJoins ()

Return join parts of the query

public Phalcon\Mvc\Model\Query\Builder where (mixed $conditions, [array $bindParams], [array $bindTypes])

Sets the query conditions

  1. <?php
  2. $builder->where(100);
  3. $builder->where("name = 'Peter'");
  4. $builder->where(
  5. "name = :name: AND id > :id:",
  6. [
  7. "name" => "Peter",
  8. "id" => 100,
  9. ]
  10. );

public Phalcon\Mvc\Model\Query\Builder andWhere (string $conditions, [array $bindParams], [array $bindTypes])

Appends a condition to the current conditions using a AND operator

  1. <?php
  2. $builder->andWhere("name = 'Peter'");
  3. $builder->andWhere(
  4. "name = :name: AND id > :id:",
  5. [
  6. "name" => "Peter",
  7. "id" => 100,
  8. ]
  9. );

public Phalcon\Mvc\Model\Query\Builder orWhere (string $conditions, [array $bindParams], [array $bindTypes])

Appends a condition to the current conditions using an OR operator

  1. <?php
  2. $builder->orWhere("name = 'Peter'");
  3. $builder->orWhere(
  4. "name = :name: AND id > :id:",
  5. [
  6. "name" => "Peter",
  7. "id" => 100,
  8. ]
  9. );

public betweenWhere (mixed $expr, mixed $minimum, mixed $maximum, [mixed $operator])

Appends a BETWEEN condition to the current conditions

  1. <?php
  2. $builder->betweenWhere("price", 100.25, 200.50);

public notBetweenWhere (mixed $expr, mixed $minimum, mixed $maximum, [mixed $operator])

Appends a NOT BETWEEN condition to the current conditions

  1. <?php
  2. $builder->notBetweenWhere("price", 100.25, 200.50);

public inWhere (mixed $expr, array $values, [mixed $operator])

Appends an IN condition to the current conditions

  1. <?php
  2. $builder->inWhere("id", [1, 2, 3]);

public notInWhere (mixed $expr, array $values, [mixed $operator])

Appends a NOT IN condition to the current conditions

  1. <?php
  2. $builder->notInWhere("id", [1, 2, 3]);

public string | array getWhere ()

Return the conditions for the query

public Phalcon\Mvc\Model\Query\Builder orderBy (string | array $orderBy)

Sets an ORDER BY condition clause

  1. <?php
  2. $builder->orderBy("Robots.name");
  3. $builder->orderBy(["1", "Robots.name"]);

public string | array getOrderBy ()

Returns the set ORDER BY clause

public having (mixed $having)

Sets a HAVING condition clause. You need to escape PHQL reserved words using [ and ] delimiters

  1. <?php
  2. $builder->having("SUM(Robots.price) > 0");

public forUpdate (mixed $forUpdate)

Sets a FOR UPDATE clause

  1. <?php
  2. $builder->forUpdate(true);

public string | array getHaving ()

Return the current having clause

public limit (mixed $limit, [mixed $offset])

Sets a LIMIT clause, optionally an offset clause

  1. <?php
  2. $builder->limit(100);
  3. $builder->limit(100, 20);
  4. $builder->limit("100", "20");

public string | array getLimit ()

Returns the current LIMIT clause

public offset (mixed $offset)

Sets an OFFSET clause

  1. <?php
  2. $builder->offset(30);

public string | array getOffset ()

Returns the current OFFSET clause

public Phalcon\Mvc\Model\Query\Builder groupBy (string | array $group)

Sets a GROUP BY clause

  1. <?php
  2. $builder->groupBy(
  3. [
  4. "Robots.name",
  5. ]
  6. );

public string getGroupBy ()

Returns the GROUP BY clause

final public string getPhql ()

Returns a PHQL statement built based on the builder parameters

public getQuery ()

Returns the query built

final public autoescape (mixed $identifier)

Automatically escapes identifiers but only if they need to be escaped.