Documentation

內容目录

上一个主题

< Class Phalcon\Mvc\Model\Query

下一个主题

Abstract class Phalcon\Mvc\Model\Query\Lang >

本页

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

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

Methods

public __construct ([unknown $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 (unknown $distinct)

Sets SELECT DISTINCT / SELECT ALL flag

<?php

$builder->distinct("status");
$builder->distinct(null);

public getDistinct ()

Returns SELECT DISTINCT / SELECT ALL flag

public columns (unknown $columns)

Sets the columns to be queried

<?php

$builder->columns("id, name");
$builder->columns(array('id', 'name'));
  $builder->columns(array('name', 'number' => 'COUNT(*)'));

public string|array getColumns ()

Return the columns to be queried

public from (unknown $models)

Sets the models who makes part of the query

<?php

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

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

Add a model to take part of the query

<?php

  // 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']);

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 a INNER join to the query

<?php

  // 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');

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

Adds a INNER join to the query

<?php

  // 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');

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

Adds a LEFT join to the query

<?php

$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

<?php

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

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

Sets the query conditions

<?php

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

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

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 $conditions, [array $bindParams], [array $bindTypes])

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 betweenWhere (unknown $expr, unknown $minimum, unknown $maximum)

Appends a BETWEEN condition to the current conditions

<?php

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

public notBetweenWhere (unknown $expr, unknown $minimum, unknown $maximum)

Appends a NOT BETWEEN condition to the current conditions

<?php

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

public inWhere (unknown $expr, array $values)

Appends an IN condition to the current conditions

<?php

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

public notInWhere (unknown $expr, array $values)

Appends a NOT IN condition to the current conditions

<?php

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

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

<?php

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

public forUpdate (unknown $forUpdate)

Sets a FOR UPDATE clause

<?php

$builder->forUpdate(true);

public string|array getHaving ()

Return the current having clause

public limit ([unknown $limit], [unknown $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 offset (unknown $offset)

Sets an OFFSET clause

<?php

$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

<?php

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

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

Follow along: