implements Phalcon\Mvc\Model\Query\BuilderInterface, Phalcon\Di\InjectionAwareInterface
Helps to create PHQL queries using an OO interface
$params = array( 'models' => array('Users'), 'columns' => array('id', 'name', 'status'), 'conditions' => array( array( "created > :min: AND created < :max:", array("min" => '2013-01-01', 'max' => '2014-01-01'), array("min" => PDO::PARAM_STR, 'max' => PDO::PARAM_STR), ), ), // or 'conditions' => "created > '2013-01-01' AND created < '2014-01-01'", 'group' => array('id', 'name'), 'having' => "name = 'Kamil'", 'order' => array('name', 'id'), 'limit' => 20, 'offset' => 20, // or 'limit' => array(20, 20), ); $queryBuilder = new \Phalcon\Mvc\Model\Query\Builder($params);
Phalcon\Mvc\Model\Query\Builder constructor
Sets the DependencyInjector container
Returns the DependencyInjector container
Sets SELECT DISTINCT / SELECT ALL flag
$builder->distinct("status"); $builder->distinct(null);
Returns SELECT DISTINCT / SELECT ALL flag
Sets the columns to be queried
$builder->columns("id, name"); $builder->columns(array('id', 'name')); $builder->columns(array('name', 'number' => 'COUNT(*)'));
Return the columns to be queried
Sets the models who makes part of the query
$builder->from('Robots'); $builder->from(array('Robots', 'RobotsParts')); $builder->from(array('r' => 'Robots', 'rp' => 'RobotsParts'));
Add a model to take part of the query
// Load data from models Robots $builder->addFrom('Robots'); // Load data from model 'Robots' using 'r' as alias in PHQL $builder->addFrom('Robots', 'r'); // Load data from model 'Robots' using 'r' as alias in PHQL // and eager load model 'RobotsParts' $builder->addFrom('Robots', 'r', 'RobotsParts'); // Load data from model 'Robots' using 'r' as alias in PHQL // and eager load models 'RobotsParts' and 'Parts' $builder->addFrom('Robots', 'r', ['RobotsParts', 'Parts']);
Return the models who makes part of the query
Adds an INNER join to the query
// Inner Join model 'Robots' with automatic conditions and alias $builder->join('Robots'); // Inner Join model 'Robots' specifing conditions $builder->join('Robots', 'Robots.id = RobotsParts.robots_id'); // Inner Join model 'Robots' specifing conditions and alias $builder->join('Robots', 'r.id = RobotsParts.robots_id', 'r'); // Left Join model 'Robots' specifing conditions, alias and type of join $builder->join('Robots', 'r.id = RobotsParts.robots_id', 'r', 'LEFT');
Adds an INNER join to the query
// Inner Join model 'Robots' with automatic conditions and alias $builder->innerJoin('Robots'); // Inner Join model 'Robots' specifing conditions $builder->innerJoin('Robots', 'Robots.id = RobotsParts.robots_id'); // Inner Join model 'Robots' specifing conditions and alias $builder->innerJoin('Robots', 'r.id = RobotsParts.robots_id', 'r');
Adds a LEFT join to the query
$builder->leftJoin('Robots', 'r.id = RobotsParts.robots_id', 'r');
Adds a RIGHT join to the query
$builder->rightJoin('Robots', 'r.id = RobotsParts.robots_id', 'r');
Return join parts of the query
Sets the query conditions
$builder->where(100); $builder->where('name = "Peter"'); $builder->where('name = :name: AND id > :id:', array('name' => 'Peter', 'id' => 100));
Appends a condition to the current conditions using a AND operator
$builder->andWhere('name = "Peter"'); $builder->andWhere('name = :name: AND id > :id:', array('name' => 'Peter', 'id' => 100));
Appends a condition to the current conditions using an OR operator
$builder->orWhere('name = "Peter"'); $builder->orWhere('name = :name: AND id > :id:', array('name' => 'Peter', 'id' => 100));
Appends a BETWEEN condition to the current conditions
$builder->betweenWhere('price', 100.25, 200.50);
Appends a NOT BETWEEN condition to the current conditions
$builder->notBetweenWhere('price', 100.25, 200.50);
Appends an IN condition to the current conditions
$builder->inWhere('id', [1, 2, 3]);
Appends a NOT IN condition to the current conditions
$builder->notInWhere('id', [1, 2, 3]);
Return the conditions for the query
Sets an ORDER BY condition clause
$builder->orderBy('Robots.name'); $builder->orderBy(array('1', 'Robots.name'));
Returns the set ORDER BY clause
Sets a HAVING condition clause. You need to escape PHQL reserved words using [ and ] delimiters
$builder->having('SUM(Robots.price) > 0');
Sets a FOR UPDATE clause
$builder->forUpdate(true);
Return the current having clause
Sets a LIMIT clause, optionally an offset clause
$builder->limit(100); $builder->limit(100, 20);
Returns the current LIMIT clause
Sets an OFFSET clause
$builder->offset(30);
Returns the current OFFSET clause
Sets a GROUP BY clause
$builder->groupBy(array('Robots.name'));
Returns the GROUP BY clause
Returns a PHQL statement built based on the builder parameters
Returns the query built
Automatically escapes identifiers but only if they need to be escaped.
© 2011–2016 Phalcon Framework Team
Licensed under the Creative Commons Attribution License 3.0.
https://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Model_Query_Builder.html