public function get(\Phalcon\Mvc\Model\Query\Builder $builder, $page) { $config = $this->di->getConfig()->pager; // TODO: obkumać dlaczego przestało działać, może pojawi się nowa wersja inkubatora. $adapter = new \Phalcon\Paginator\Adapter\QueryBuilder(array('builder' => $builder, 'limit' => $config->limit, 'page' => $page)); $adapter = new \Phalcon\Paginator\Adapter\Model(array('data' => $builder->getQuery()->execute(), 'limit' => intval($config->limit), 'page' => $page)); return new \Phalcon\Paginator\Pager($adapter, array('layoutClass' => 'Phalcon\\Paginator\\Pager\\Layout\\Bootstrap', 'rangeLength' => intval($config->length), 'urlMask' => '?page={%page_number}')); }
/** * Initialize form. * * @return void */ public function initialize() { $builder = new Builder(); $builder->columns(['scope'])->from('Core\\Model\\LanguageTranslation')->distinct(true); $result = $builder->getQuery()->execute(); $data = []; foreach ($result as $row) { $data[$row->scope] = $row->scope; } $this->addMultiSelect('scope', 'Scope', 'Select scopes of translations to export.', $data, $data); }
/** * 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); }
/** * @param \stdClass $pager * @param Query\Builder $builder * @param integer $limit * @return \stdClass * * 因为Phalcon的分页,在连表查询时候,会产生bug,导致分页出错,所以用此方法来对其重新赋值 */ public function correctPaginator(\stdClass $pager, \Phalcon\Mvc\Model\Query\Builder $builder, $limit) { $builderArray = $builder->getQuery()->execute()->toArray(); $newPaginator = new PurePaginator($limit, count($builderArray), $builderArray); $pager->before = $newPaginator->before; $pager->first = $newPaginator->first; $pager->next = $newPaginator->next; $pager->last = $newPaginator->last; $pager->current = $newPaginator->current; $pager->total_items = $newPaginator->total_items; $pager->total_pages = $newPaginator->total_pages; $pager->page_range = $newPaginator->page_range; $pager->next_range = $newPaginator->next_range; $pager->prev_range = $newPaginator->prev_range; return $pager; }
/** * 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; }