Documentation

Class Phalcon\Mvc\Model\Query\Builder\Select

extends abstract class Phalcon\Mvc\Model\Query\Builder\Join

implements Phalcon\Mvc\Model\Query\BuilderInterface, Phalcon\DI\InjectionAwareInterface, Phalcon\Events\EventsAwareInterface

Source on GitHub

<?php

$resultset = $this->modelsManager->createBuilder()
   ->from('Robots')
   ->join('RobotsParts')
   ->limit(20)
   ->orderBy('Robots.name')
   ->getQuery()
   ->execute();

Methods

public __construct ([array $params])

Phalcon\Mvc\Model\Query\Builder\Select constructor

<?php

 $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\Select($params);

public Phalcon\Mvc\Model\Query\Builder\Select distinct (bool|null $distinct)

Sets SELECT DISTINCT / SELECT ALL flag

public boolean getDistinct ()

Returns SELECT DISTINCT / SELECT ALL flag

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

Sets the columns to be queried

<?php

$builder->columns(array('id', 'name'));

public string|array getColumns ()

Return the columns to be queried

public Phalcon\Mvc\Model\Query\Builder\Select from (string|array $model, [unknown $alias], [unknown $merge])

Sets the models who makes part of the query

<?php

$builder->from('Robots');
$builder->from(array('Robots', 'RobotsParts'));

public Phalcon\Mvc\Model\Query\Builder\Select addFrom (string|array $model, [string $alias])

Add a model to take part of the query

<?php

$builder->addFrom('Robots', 'r');

public array getFrom ()

Return the models who makes part of the query

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

Sets a ORDER BY condition clause

<?php

$builder->orderBy('Robots.name');
$builder->orderBy(array('1', 'Robots.name'));

public string|array getOrderBy ()

Returns the set ORDER BY clause

public Phalcon\Mvc\Model\Query\Builder\Select having (string $having)

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

<?php

$builder->having('SUM(Robots.price) > 0');

public string|array getHaving ()

Return the current having clause

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

Sets a LIMIT clause, optionally a offset clause

<?php

$builder->limit(100);
$builder->limit(100, 20);

public string|array getLimit ()

Returns the current LIMIT clause

public Phalcon\Mvc\Model\Query\Builder\Select offset (int $offset)

Sets an OFFSET clause

<?php

$builder->offset(30);

public string|array getOffset ()

Returns the current OFFSET clause

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

Sets a GROUP BY clause

<?php

$builder->groupBy(array('Robots.name'));

public string|array getGroupBy ()

Returns the GROUP BY clause

protected string _compile ()

Returns a PHQL statement built based on the builder parameters

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

Adds a join to the query

<?php

$builder->join('Robots');
$builder->join('Robots', 'r.id = RobotsParts.robots_id');
$builder->join('Robots', 'r.id = RobotsParts.robots_id', 'r');
$builder->join('Robots', 'r.id = RobotsParts.robots_id', 'r', 'LEFT');

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

Adds a INNER join to the query

<?php

$builder->innerJoin('Robots');
$builder->innerJoin('Robots', 'r.id = RobotsParts.robots_id');
$builder->innerJoin('Robots', 'r.id = RobotsParts.robots_id', 'r');

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

Adds a LEFT join to the query

<?php

$builder->leftJoin('Robots', 'r.id = RobotsParts.robots_id', 'r');

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

Adds a RIGHT join to the query

<?php

$builder->rightJoin('Robots', 'r.id = RobotsParts.robots_id', 'r');

public int setConditions (unknown $conditions, [array $bindParams], [array $bindTypes], [array $bindParams], [unknown $type]) inherited from Phalcon\Mvc\Model\Query\Builder\Where

Gets the type of PHQL queries

public string getConditions () inherited from Phalcon\Mvc\Model\Query\Builder\Where

Returns the conditions, If the conditions is a single numeric field. We internally create a condition using the related primary key

<?php

$builder->getConditions();

public Phalcon\Mvc\Model\Query\Builder where (string|array $conditions, [array $bindParams], [array $bindTypes]) inherited from Phalcon\Mvc\Model\Query\Builder\Where

Sets the query conditions

<?php

$builder->where('name = "Peter"');
$builder->where('name = :name: AND id > :id:', array('name' => 'Peter', 'id' => 100));

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

Appends a condition to the current conditions using a AND operator

<?php

$builder->andWhere('name = "Peter"');
$builder->andWhere('name = :name: AND id > :id:', array('name' => 'Peter', 'id' => 100));

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

Appends a condition to the current conditions using a OR operator

<?php

$builder->orWhere('name = "Peter"');
$builder->orWhere('name = :name: AND id > :id:', array('name' => 'Peter', 'id' => 100));

public Phalcon\Mvc\Model\Query\Builder betweenWhere (string $expr, mixed $minimum, mixed $maximum, [boolean $useOrWhere]) inherited from Phalcon\Mvc\Model\Query\Builder\Where

Appends a BETWEEN condition to the current conditions

<?php

$builder->betweenWhere('price', 100.25, 200.50);

public Phalcon\Mvc\Model\Query\Builder notBetweenWhere (string $expr, mixed $minimum, mixed $maximum, [boolean $useOrWhere]) inherited from Phalcon\Mvc\Model\Query\Builder\Where

Appends a NOT BETWEEN condition to the current conditions

<?php

$builder->notBetweenWhere('price', 100.25, 200.50);

public Phalcon\Mvc\Model\Query\Builder inWhere (string $expr, array $values, [boolean $useOrWhere]) inherited from Phalcon\Mvc\Model\Query\Builder\Where

Appends an IN condition to the current conditions

<?php

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

public Phalcon\Mvc\Model\Query\Builder notInWhere (string $expr, array $values, [boolean $useOrWhere]) inherited from Phalcon\Mvc\Model\Query\Builder\Where

Appends a NOT IN condition to the current conditions

<?php

$builder->notInWhere('id', [1, 2, 3]);

public string|array getWhere () inherited from Phalcon\Mvc\Model\Query\Builder\Where

Return the conditions for the query

public static Phalcon\Mvc\Model\Query\Builder create (unknown $type) inherited from Phalcon\Mvc\Model\Query\Builder

Create a new Query Builder of the given type.

<?php

Phalcon\Mvc\Model\Query\Builder::create(Phalcon\Mvc\Model\Query::TYPE_SELECT);

public static Phalcon\Mvc\Model\Query\Builder\Select createSelectBuilder () inherited from Phalcon\Mvc\Model\Query\Builder

Create a new Query Builder for Select

public static Phalcon\Mvc\Model\Query\Builder\Insert createInsertBuilder () inherited from Phalcon\Mvc\Model\Query\Builder

Create a new Query Builder for Insert

public static Phalcon\Mvc\Model\Query\Builder\Update createUpdateBuilder () inherited from Phalcon\Mvc\Model\Query\Builder

Create a new Query Builder for Update

public static Phalcon\Mvc\Model\Query\Builder\Delete createDeleteBuilder () inherited from Phalcon\Mvc\Model\Query\Builder

Create a new Query Builder for Delete

public int getType () inherited from Phalcon\Mvc\Model\Query\Builder

Gets the type of PHQL queries

public Phalcon\Mvc\Model\Query\Builder setBindParams (array $bindparams, [unknown $merge]) inherited from Phalcon\Mvc\Model\Query\Builder

Sets the bind parameters

public Phalcon\Mvc\Model\Query\Builder getBindParams () inherited from Phalcon\Mvc\Model\Query\Builder

Gets the bind parameters

public Phalcon\Mvc\Model\Query\Builder getMergeBindParams () inherited from Phalcon\Mvc\Model\Query\Builder

Gets the merge bind parameters

public Phalcon\Mvc\Model\Query\Builder setBindTypes (array $bindtypes, [unknown $merge]) inherited from Phalcon\Mvc\Model\Query\Builder

Sets the bind types

public Phalcon\Mvc\Model\Query\Builder getBindTypes () inherited from Phalcon\Mvc\Model\Query\Builder

Gets the bind types

public Phalcon\Mvc\Model\Query\Builder getMergeBindTypes () inherited from Phalcon\Mvc\Model\Query\Builder

Gets the merge bind types

public Phalcon\Mvc\Model\Query\Builder compile () inherited from Phalcon\Mvc\Model\Query\Builder

Compile the PHQL query

public string getPhql () inherited from Phalcon\Mvc\Model\Query\Builder

Returns a PHQL statement built based on the builder parameters

public Phalcon\Mvc\Model\Query getQuery () inherited from Phalcon\Mvc\Model\Query\Builder

Returns the query built

public setDI (Phalcon\DIInterface $dependencyInjector) inherited from Phalcon\DI\Injectable

Sets the dependency injector

public Phalcon\DIInterface getDI ([unknown $error], [unknown $notUseDefault]) inherited from Phalcon\DI\Injectable

Returns the internal dependency injector

public setEventsManager (Phalcon\Events\ManagerInterface $eventsManager) inherited from Phalcon\DI\Injectable

Sets the event manager

public Phalcon\Events\ManagerInterface getEventsManager () inherited from Phalcon\DI\Injectable

Returns the internal event manager

public boolean fireEvent (string $eventName, [unknown $data], [unknown $cancelable]) inherited from Phalcon\DI\Injectable

Fires an event, implicitly calls behaviors and listeners in the events manager are notified

public boolean fireEventCancel (string $eventName, [unknown $data], [unknown $cancelable]) inherited from Phalcon\DI\Injectable

Fires an event, implicitly calls behaviors and listeners in the events manager are notified This method stops if one of the callbacks/listeners returns boolean false

public boolean hasService (string $name) inherited from Phalcon\DI\Injectable

Check whether the DI contains a service by a name

public mixed getResolveService (string $name, [unknown $args], [unknown $noerror], [unknown $noshared]) inherited from Phalcon\DI\Injectable

Resolves the service based on its configuration

public __get (unknown $property) inherited from Phalcon\DI\Injectable

Magic method __get

public __sleep () inherited from Phalcon\DI\Injectable

...

public __debugInfo () inherited from Phalcon\DI\Injectable

...