public function findMeetingsThisMonth(Query $query, $options = []) { $defaultOptions = ['results' => 5]; $options = array_merge($defaultOptions, $options); // override defaultoptions $dateFrom = new DateTime('first day of this month'); $query->hydrate(false)->select(['Users.name', 'totalMeetings' => $query->func()->count('Meetings.id')])->matching('Meetings')->where(['Meetings.date >=' => $dateFrom->format('Y-m-d')])->group(['Users.name'])->orderDesc('totalMeetings')->limit($options['results']); return $query; }
public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) { if (!$query->hydrate()) { return; } $query->formatResults(function ($results) { return $results->map(function ($row) { $type = $row[$this->config('typeField')]; $entityClass = $this->_typeMap[$type]['entityClass']; return new $entityClass($row->forCopy(), ['markNew' => $row->isNew(), 'markClean' => true, 'guard' => false, 'source' => $this->_typeMap[$type]['alias']]); }); }); }
/** * Constructor * * @param \Cake\ORM\Query $query Query from where results come * @param \Cake\Database\StatementInterface $statement */ public function __construct($query, $statement) { $repository = $query->repository(); $this->_query = $query; $this->_statement = $statement; $this->_defaultTable = $this->_query->repository(); $this->_calculateAssociationMap(); $this->_hydrate = $this->_query->hydrate(); $this->_entityClass = $repository->entityClass(); $this->_useBuffering = $query->bufferResults(); if ($statement) { $this->count(); } }
/** * Constructor * * @param \Cake\ORM\Query $query Query from where results come * @param \Cake\Database\StatementInterface $statement The statement to fetch from */ public function __construct($query, $statement) { $repository = $query->repository(); $this->_query = $query; $this->_statement = $statement; $this->_driver = $driver = $this->_query->connection()->driver(); $this->_defaultTable = $this->_query->repository(); $this->_calculateAssociationMap(); $this->_hydrate = $this->_query->hydrate(); $this->_entityClass = $repository->entityClass(); $this->_useBuffering = $query->bufferResults(); $this->_defaultAlias = $this->_defaultTable->alias(); $this->_calculateColumnMap(); $this->_calculateTypeMap(); if ($this->_useBuffering) { $count = $this->count(); $this->_results = new SplFixedArray($count); } }
/** * Builds an array containing the results from fetchQuery indexed by * the foreignKey value corresponding to this association. * * @param \Cake\ORM\Query $fetchQuery The query to get results from * @param array $options The options passed to the eager loader * @return array * @throws \RuntimeException when the association property is not part of the results set. */ protected function _buildResultMap($fetchQuery, $options) { $resultMap = []; $key = (array) $options['foreignKey']; $property = $this->target()->association($this->junction()->alias())->property(); $hydrated = $fetchQuery->hydrate(); foreach ($fetchQuery->all() as $result) { if (!isset($result[$property])) { throw new RuntimeException(sprintf('"%s" is missing from the belongsToMany results. Results cannot be created.', $property)); } $result[$this->_junctionProperty] = $result[$property]; unset($result[$property]); if ($hydrated) { $result->dirty($this->_junctionProperty, false); } $values = []; foreach ($key as $k) { $values[] = $result[$this->_junctionProperty][$k]; } $resultMap[implode(';', $values)][] = $result; } return $resultMap; }
/** * Tests that first can be called on an unbuffered query * * @return void */ public function testFirstUnbuffered() { $table = TableRegistry::get('Articles'); $query = new Query($this->connection, $table); $query->select(['id']); $first = $query->hydrate(false)->bufferResults(false)->first(); $this->assertEquals(['id' => 1], $first); }
/** * Builds an array containing the results from fetchQuery indexed by * the foreignKey value corresponding to this association. * * @param \Cake\ORM\Query $fetchQuery The query to get results from * @param array $options The options passed to the eager loader * @return array */ protected function _buildResultMap($fetchQuery, $options) { $resultMap = []; $key = (array) $options['foreignKey']; $property = $this->target()->association($this->junction()->alias())->property(); $hydrated = $fetchQuery->hydrate(); foreach ($fetchQuery->all() as $result) { $result[$this->_junctionProperty] = $result[$property]; unset($result[$property]); if ($hydrated) { $result->dirty($this->_junctionProperty, false); } $values = []; foreach ($key as $k) { $values[] = $result[$this->_junctionProperty][$k]; } $resultMap[implode(';', $values)][] = $result; } return $resultMap; }
/** * Tests that first() will not execute the same query twice * * @return void */ public function testFirstSameResult() { $table = TableRegistry::get('articles', ['table' => 'articles']); $query = new Query($this->connection, $table); $query->select(['id'])->toArray(); $first = $query->hydrate(false)->first(); $resultSet = $query->all(); $this->assertEquals(['id' => 1], $first); $this->assertSame($resultSet, $query->all()); }