Class Phalcon\Mvc\Model\Query\Builder

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

Helps to create PHQL queries using an OO interface

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

Methods

public __construct ([unknown $params], [unknown $dependencyInjector])

Phalcon\Mvc\Model\Query\Builder constructor

public Phalcon\Mvc\Model\Query\Builder setDI (unknown $dependencyInjector)

Sets the DependencyInjector container

public Phalcon\DiInterface getDI ()

Returns the DependencyInjector container

public Phalcon\Mvc\Model\Query\BuilderInterface distinct (unknown $distinct)

Sets SELECT DISTINCT / SELECT ALL flag

public bool getDistinct ()

Returns SELECT DISTINCT / SELECT ALL flag

public Phalcon\Mvc\Model\Query\Builder columns (unknown $columns)

Sets the columns to be queried

  1. <?php
  2. $builder->columns(array('id', 'name'));

public string|array getColumns ()

Return the columns to be queried

public Phalcon\Mvc\Model\Query\Builder from (unknown $models)

Sets the models who makes part of the query

  1. <?php
  2. $builder->from('Robots');
  3. $builder->from(array('Robots', 'RobotsParts'));

public Phalcon\Mvc\Model\Query\Builder addFrom (unknown $model, [unknown $alias])

Add a model to take part of the query

  1. <?php
  2. $builder->addFrom('Robots', 'r');

public string|array getFrom ()

Return the models who makes part of the query

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

Adds a INNER join to the query

  1. <?php
  2. $builder->join('Robots');
  3. $builder->join('Robots', 'r.id = RobotsParts.robots_id');
  4. $builder->join('Robots', 'r.id = RobotsParts.robots_id', 'r');
  5. $builder->join('Robots', 'r.id = RobotsParts.robots_id', 'r', 'INNER');

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

Adds a INNER join to the query

  1. <?php
  2. $builder->innerJoin('Robots');
  3. $builder->innerJoin('Robots', 'r.id = RobotsParts.robots_id');
  4. $builder->innerJoin('Robots', 'r.id = RobotsParts.robots_id', 'r');

public Phalcon\Mvc\Model\Query\Builder leftJoin (unknown $model, [unknown $conditions], [unknown $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 (unknown $model, [unknown $conditions], [unknown $alias])

Adds a RIGHT join to the query

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

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

Sets the query conditions

  1. <?php
  2. $builder->where(100);
  3. $builder->where('name = "Peter"');
  4. $builder->where('name = :name: AND id > :id:', array('name' => 'Peter', 'id' => 100));

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

Appends a condition to the current conditions using a AND operator

  1. <?php
  2. $builder->andWhere('name = "Peter"');
  3. $builder->andWhere('name = :name: AND id > :id:', array('name' => 'Peter', 'id' => 100));

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

Appends a condition to the current conditions using a OR operator

  1. <?php
  2. $builder->orWhere('name = "Peter"');
  3. $builder->orWhere('name = :name: AND id > :id:', array('name' => 'Peter', 'id' => 100));

public Phalcon\Mvc\Model\Query\Builder betweenWhere (unknown $expr, unknown $minimum, unknown $maximum)

Appends a BETWEEN condition to the current conditions

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

public Phalcon\Mvc\Model\Query\Builder notBetweenWhere (unknown $expr, unknown $minimum, unknown $maximum)

Appends a NOT BETWEEN condition to the current conditions

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

public Phalcon\Mvc\Model\Query\Builder inWhere (unknown $expr, unknown $values)

Appends an IN condition to the current conditions

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

public Phalcon\Mvc\Model\Query\Builder notInWhere (unknown $expr, unknown $values)

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 (unknown $orderBy)

Sets a ORDER BY condition clause

  1. <?php
  2. $builder->orderBy('Robots.name');
  3. $builder->orderBy(array('1', 'Robots.name'));

public string|array getOrderBy ()

Returns the set ORDER BY clause

public Phalcon\Mvc\Model\Query\Builder having (unknown $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 string|array getHaving ()

Return the current having clause

public Phalcon\Mvc\Model\Query\Builder limit (unknown $limit, [unknown $offset])

Sets a LIMIT clause, optionally a offset clause

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

public string|array getLimit ()

Returns the current LIMIT clause

public Phalcon\Mvc\Model\Query\Builder offset (unknown $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 (unknown $group)

Sets a GROUP BY clause

  1. <?php
  2. $builder->groupBy(array('Robots.name'));

public string getGroupBy ()

Returns the GROUP BY clause

public string getPhql ()

Returns a PHQL statement built based on the builder parameters

public Phalcon\Mvc\Model\Query getQuery ()

Returns the query built