/** * Find model instances by ids * @param int[] $ids * @return mixed */ public function findMany($ids) { $query = new Builder(null); $query->setDI($this->di); $query->from($this->class); return $query->inWhere('id', $ids)->getQuery()->execute(); }
/** * @param string $fullUrl Full url. * @param array $params * @return \Phalcon\Mvc\Model\Query */ public static function buildQuery($fullUrl, array $params = array()) { /** * @var \Phalcon\Di $di */ global $di; /** * @var \Phalcon\Mvc\Model $modelName */ $modelName = Text::camelize(Text::uncamelize(Analyzer::getModelName($fullUrl))); $builder = new Builder(); $builder->setDI($di); $builder->from($modelName); self::setColumns($builder, $params); $prepareUrl = self::getPrepareUrlOperators($fullUrl); $whereSql = self::buildWhere($prepareUrl); $whereSqlReplacement = Macros::replace($whereSql, $params, $modelName); $builder->where($whereSqlReplacement); $builder = self::limit($builder, $params); $builder = self::orderBy($builder, $params); $params = new Params($fullUrl, $params); $params = $params->getPrepareParams(); $query = $builder->getQuery(); $query->setBindParams($params, true); return $query; }
/** * Get builder associated with table of this model. * * @param string|null $tableAlias Table alias to use in query. * * @return Builder */ public static function getBuilder($tableAlias = null) { $builder = new Builder(); $table = get_called_class(); if (!$tableAlias) { $builder->from($table); } else { $builder->addFrom($table, $tableAlias); } return $builder; }
/** * Get main select builder. * * @return Builder */ public function getSource() { $builder = new Builder(); $builder->from('Core\\Model\\LanguageTranslation')->where('language_id = ' . $this->_language->getId()); $showUntranslated = (bool) $this->getDI()->getRequest()->get('untranslated', 'int', 0); if ($showUntranslated) { $builder->where("original = translated"); } if ($search = $this->getDI()->getRequest()->get('search')) { $builder->where("original LIKE '%{$search}%'")->orWhere("translated LIKE '%{$search}%'"); } return $builder; }
/** * Test checks passing 'condition' query param into constructor. * Conditions can now be passed as an string(as before) and * as an array of 3 elements: * - condition string for example "age > :age: AND created > :created:" * - bind params for example array('age' => 18, 'created' => '2013-09-01') * - bind types for example array('age' => PDO::PARAM_INT, 'created' => PDO::PARAM_STR) * * First two params are REQUIRED, bind types are optional. */ public function testConstructorConditions() { require 'unit-tests/config.db.php'; if (empty($configMysql)) { $this->markTestSkipped("Test skipped"); return; } // ------------- test for setters(classic) way ---------------- $standardBuilder = new Builder(); $standardBuilder->from('Robots')->where("year > :min: AND year < :max:", array("min" => '2013-01-01', 'max' => '2100-01-01'), array("min" => PDO::PARAM_STR, 'max' => PDO::PARAM_STR)); $standardResult = $standardBuilder->getQuery()->execute(); // --------------- test for single condition ------------------ $params = array('models' => 'Robots', 'conditions' => array(array("year > :min: AND year < :max:", array("min" => '2013-01-01', 'max' => '2100-01-01'), array("min" => PDO::PARAM_STR, 'max' => PDO::PARAM_STR)))); $builderWithSingleCondition = new Builder($params); $singleConditionResult = $builderWithSingleCondition->getQuery()->execute(); // ------------- test for multiple conditions ---------------- $params = array('models' => 'Robots', 'conditions' => array(array("year > :min:", array("min" => '2000-01-01'), array("min" => PDO::PARAM_STR)), array("year < :max:", array('max' => '2100-01-01'), array("max" => PDO::PARAM_STR)))); // conditions are merged! $builderMultipleConditions = new Builder($params); $multipleConditionResult = $builderMultipleConditions->getQuery()->execute(); $expectedPhql = "SELECT [Robots].* FROM [Robots] " . "WHERE year > :min: AND year < :max:"; /* ------------ ASSERTING --------- */ $this->assertEquals($expectedPhql, $standardBuilder->getPhql()); $this->assertInstanceOf("Phalcon\\Mvc\\Model\\Resultset\\Simple", $standardResult); $this->assertEquals($expectedPhql, $builderWithSingleCondition->getPhql()); $this->assertInstanceOf("Phalcon\\Mvc\\Model\\Resultset\\Simple", $singleConditionResult); $this->assertEquals($expectedPhql, $builderMultipleConditions->getPhql()); $this->assertInstanceOf("Phalcon\\Mvc\\Model\\Resultset\\Simple", $multipleConditionResult); }
/** * Get main select builder. * * @return Builder */ public function getSource() { $builder = new Builder(); $builder->from(['u' => 'User\\Model\\User'])->columns(['u.*', 'r.name'])->leftJoin('User\\Model\\Role', 'u.role_id = r.id', 'r')->orderBy('u.id DESC'); return $builder; }
/** * Get main select builder. * * @return Builder */ public function getSource() { $builder = new Builder(); $builder->from('User\\Model\\Role'); return $builder; }
/** * Get main select builder. * * @return Builder */ public function getSource() { $builder = new Builder(); $builder->from('Core\\Model\\Menu')->columns(['id', 'name']); return $builder; }
/** * Get main select builder. * * @return Builder */ public function getSource() { $builder = new Builder(); $builder->from('Core\\Model\\Page'); return $builder; }
/** * Test checks passing 'condition' query param into constructor. * Conditions can now be passed as an string(as before) and * as an array of 3 elements: * - condition string for example "age > :age: AND created > :created:" * - bind params for example array('age' => 18, 'created' => '2013-09-01') * - bind types for example array('age' => PDO::PARAM_INT, 'created' => PDO::PARAM_STR) * * First two params are REQUIRED, bind types are optional. */ public function testConstructorConditions() { $this->specify("Query Builders don't work with conditions specified in the constructor params", function () { // ------------- test for setters(classic) way ---------------- $standardBuilder = new Builder(); $standardBuilder->from(Robots::class)->where("year > :min: AND year < :max:", ["min" => "2013-01-01", "max" => "2100-01-01"], ["min" => \PDO::PARAM_STR, "max" => \PDO::PARAM_STR]); $standardResult = $standardBuilder->getQuery()->execute(); // --------------- test for single condition ------------------ $params = ["models" => Robots::class, "conditions" => [["year > :min: AND year < :max:", ["min" => "2013-01-01", "max" => "2100-01-01"], ["min" => \PDO::PARAM_STR, "max" => \PDO::PARAM_STR]]]]; $builderWithSingleCondition = new Builder($params); $singleConditionResult = $builderWithSingleCondition->getQuery()->execute(); // ------------- test for multiple conditions ---------------- $params = ["models" => Robots::class, "conditions" => [["year > :min:", ["min" => "2000-01-01"], ["min" => \PDO::PARAM_STR]], ["year < :max:", ["max" => "2100-01-01"], ["max" => \PDO::PARAM_STR]]]]; // conditions are merged! $builderMultipleConditions = new Builder($params); $multipleConditionResult = $builderMultipleConditions->getQuery()->execute(); $expectedPhql = "SELECT [" . Robots::class . "].* FROM [" . Robots::class . "] WHERE year > :min: AND year < :max:"; /* ------------ ASSERTING --------- */ expect($standardBuilder->getPhql())->equals($expectedPhql); expect($standardResult)->isInstanceOf("Phalcon\\Mvc\\Model\\Resultset\\Simple"); expect($builderWithSingleCondition->getPhql())->equals($expectedPhql); expect($singleConditionResult)->isInstanceOf("Phalcon\\Mvc\\Model\\Resultset\\Simple"); expect($builderMultipleConditions->getPhql())->equals($expectedPhql); expect($multipleConditionResult)->isInstanceOf("Phalcon\\Mvc\\Model\\Resultset\\Simple"); }); }
/** * Generate a PHQL SELECT statement for an aggregate * * @param string $function * @param string $alias * @param mixed $parameters * @return \Phalcon\Mvc\Model\ResultsetInterface * @throws Exception */ protected static function _groupResult($function, $alias, $parameters = null) { if (is_string($function) === false || is_string($alias) === false) { throw new Exception('Invalid parameter type.'); } if (is_array($parameters) === false) { if (is_null($parameters) === false) { $params = array($parameters); } else { $params = array(); } } else { $params = $parameters; } if (isset($params['column']) === true) { $groupColumn = $params['column']; } else { $groupColumn = '*'; } //Builds the column to query according to the received parameters if (isset($params['distinct']) === true) { $columns = $function . '(DISTINCT ' . $params['distinct'] . ') AS ' . $alias; } else { if (isset($params['group']) === true) { $columns = $params['group'] . ', ' . $function . '(' . $params['group'] . ') AS ' . $alias; } else { $columns = $function . '(' . $groupColumn . ') AS ' . $alias; } } //Builds a query with the passed parameters $builder = new Builder($params); $builder->columns($columns); $builder->from(get_called_class()); $query = $builder->getQuery(); $bindParams = null; $bindTypes = null; //Check for bind parameters if (isset($params['bind']) === true) { $bindParams = $params['bind']; if (isset($params['bindTypes']) === true) { $bindTypes = $params['bindTypes']; } } //Execute the query $resultset = $query->execute($bindParams, $bindTypes); //Pass the cache options to the query if (isset($params['cache']) === true) { $query->cache($params['cache']); } //Return the full resultset if the query is grouped if (isset($params['group']) === true) { return $resultset; } //Return only the value in the first result //$number_rows = count($resultset); @note this variable is not necessary $firstRow = $resultset->getFirst(); return $firstRow->alias; }