本文整理汇总了PHP中Cake\ORM\Query::formatResults方法的典型用法代码示例。如果您正苦于以下问题:PHP Query::formatResults方法的具体用法?PHP Query::formatResults怎么用?PHP Query::formatResults使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\ORM\Query
的用法示例。
在下文中一共展示了Query::formatResults方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: findMap
/**
* Custom finder for map the ntofications.
*
* @param \Cake\ORM\Query $query The query finder.
* @param array $options The options passed in the query builder.
*
* @return \Cake\ORM\Query
*/
public function findMap(Query $query, array $options)
{
return $query->formatResults(function ($notifications) use($options) {
return $notifications->map(function ($notification) use($options) {
$notification->data = unserialize($notification->data);
switch ($notification->type) {
case 'conversation.reply':
$username = $notification->data['sender']->username;
$conversationTitle = Text::truncate($notification->data['conversation']->title, 50, ['ellipsis' => '...', 'exact' => false]);
//Check if the creator of the conversation is the current user.
if ($notification->data['conversation']->user_id === $options['session']->read('Auth.User.id')) {
$notification->text = __('<strong>{0}</strong> has replied in your conversation <strong>{1}</strong>.', h($username), h($conversationTitle));
} else {
$notification->text = __('<strong>{0}</strong> has replied in the conversation <strong>{1}</strong>.', h($username), h($conversationTitle));
}
$notification->link = Router::url(['controller' => 'conversations', 'action' => 'go', $notification->data['conversation']->last_message_id, 'prefix' => false]);
break;
case 'bot':
$notification->text = __('Welcome on <strong>{0}</strong>! You can now post your first comment in the blog.', \Cake\Core\Configure::read('Site.name'));
$notification->link = Router::url(['controller' => 'blog', 'action' => 'index', 'prefix' => false]);
$notification->icon = $notification->data['icon'];
break;
case 'badge':
$notification->text = __('You have unlock the badge "{0}".', $notification->data['badge']->name);
$notification->link = Router::url(['_name' => 'users-profile', 'id' => $notification->data['user']->id, 'slug' => $notification->data['user']->username, '#' => 'badges', 'prefix' => false]);
break;
}
return $notification;
});
});
}
示例2: beforeFind
public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary)
{
if (!array_key_exists('getRelated', $options) || !$options['getRelated']) {
//Jen pokud se mají related stahovat
return true;
}
$attachedTables = $this->_InRelatedIndexBehavior->getTablesWithBehaviorNames();
/** @var \Cake\ORM\Table $attachedTable */
foreach ($attachedTables as $tableName) {
$modelName = Inflector::camelize($tableName);
$query->contain(['Related' . $modelName => []]);
}
$query->formatResults(function ($results) {
return $results->map(function ($row) {
$temp = $row->toArray();
$related = [];
foreach ($temp as $key => $item) {
if (preg_match('/related-.*/', $key)) {
foreach ($row->{$key} as $id => $similar) {
$table_name = explode('-', $key);
$row->{$key}[$id]->table_name = end($table_name);
}
$related = array_merge($related, $row->{$key});
unset($row->{$key});
}
}
$row->related = $related;
return $row;
});
});
return true;
}
示例3: beforeFind
/**
* Decode the fields on after find
*
* @param \Cake\Event\Event $event
* @param \Cake\ORM\Query $query
* @return void
*/
public function beforeFind(Event $event, Query $query)
{
$query->formatResults(function (ResultSetInterface $results) {
return $results->map(function ($row) {
$this->processItems($row, 'output');
return $row;
});
});
}
示例4: beforeFind
/**
* beforeFind, starts a timer for a find operation.
*
* @param \Cake\Event\Event $event The beforeFind event
* @param \Cake\ORM\Query $query Query
* @return bool true
*/
public function beforeFind(Event $event, $query)
{
$alias = $event->subject()->alias();
DebugTimer::start($alias . '_find', $alias . '->find()');
return $query->formatResults(function ($results) use($alias) {
DebugTimer::stop($alias . '_find');
return $results;
});
}
示例5: beforeFind
public function beforeFind(Event $event, Query $query)
{
$query->formatResults(function ($results) {
return $results->map(function ($row) {
if (!$row instanceof Entity) {
return $row;
}
$this->transformState($row);
return $row;
});
});
}
示例6: beforeFind
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']]);
});
});
}
示例7: findAutoLock
/**
* Locks all the rows returned by the query.
* This finder requires the `lockingUser` key in options and optionally the
* `lockingSession`.
*
* @param Query $quert The Query to modify
* @param array|ArrayObject $options The options containing the `lockingUser` key
* @return Query
*/
public function findAutoLock(Query $query, $options)
{
$by = Hash::get($options, 'lockingUser');
$session = Hash::get($options, 'lockingSession');
return $query->formatResults(function ($results) use($by, $session) {
$results->filter(function ($r) {
return $r instanceof LockableInterface;
})->each(function ($r) use($by, $session) {
$r->lock($by, $session);
$this->_table->save($r);
});
return $results;
});
}
示例8: beforeFind
/**
* Attaches ContentType information to each content revision.
*
* @param \Cake\Event\Event $event The event that was triggered
* @param \Cake\ORM\Query $query The query object
* @param \ArrayObject $options Additional options given as an array
* @param bool $primary Whether this find is a primary query or not
* @return Query
*/
public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary)
{
$query->formatResults(function ($results) {
return $results->map(function ($revision) {
try {
if (isset($revision->data->content_type_id)) {
$contentType = TableRegistry::get('Content.ContentTypes')->find()->where(['id' => $revision->data->content_type_id])->first();
$revision->data->set('content_type', $contentType);
}
} catch (\Exception $e) {
$revision->data->set('content_type', false);
}
return $revision;
});
});
return $query;
}
示例9: beforeFind
/**
* Attaches comments to each entity on find operation.
*
* @param \Cake\Event\Event $event The event that was triggered
* @param \Cake\ORM\Query $query The query object
* @param array $options Additional options as an array
* @param bool $primary Whether is find is a primary query or not
* @return void
*/
public function beforeFind(Event $event, $query, $options, $primary)
{
if ($this->_enabled && $query->count() > 0) {
$pk = $this->_table->primaryKey();
$tableAlias = Inflector::underscore($this->_table->alias());
$query->contain(['Comments' => function ($query) {
return $query->find('threaded')->contain(['Users'])->order($this->config('order'));
}]);
if ($this->config('count') || isset($options['comments_count']) && $options['comments_count'] === true) {
$query->formatResults(function ($results) use($pk, $tableAlias) {
return $results->map(function ($entity) use($pk, $tableAlias) {
$entityId = $entity->{$pk};
$count = TableRegistry::get('Comment.Comments')->find()->where(['entity_id' => $entityId, 'table_alias' => $tableAlias])->count();
$entity->set('comments_count', $count);
return $entity;
});
});
}
}
}
示例10: findDecrypted
/**
* Custom finder to retrieve decrypted values.
*
* @param \Cake\ORM\Query $query Query.
* @param array $options Options.
* @return \Cake\ORM\Query
*/
public function findDecrypted(Query $query, array $options)
{
$options += ['fields' => []];
$mapper = function ($row) use($options) {
$driver = $this->_table->connection()->driver();
foreach ($this->config('fields') as $field => $type) {
if ($options['fields'] && !in_array($field, (array) $options['fields']) || !$row->has($field)) {
continue;
}
$cipher = $row->get($field);
$plain = $this->decrypt($cipher);
$row->set($field, Type::build($type)->toPHP($plain, $driver));
}
return $row;
};
$formatter = function ($results) use($mapper) {
return $results->map($mapper);
};
return $query->formatResults($formatter);
}
示例11: beforeFind
/**
* Decrypt values after retrieving from DB
* @param Event $event Event object
* @param Query $query Query object
* @param ArrayObject $options Query options array
* @param type $primary Root/associated query
* @return void
*/
public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary)
{
$fields = $this->config('fields');
$driver = $this->_table->connection()->driver();
$formatter = function (\Cake\Collection\CollectionInterface $results) use($fields, $driver) {
return $results->each(function ($entity) use($fields, $driver) {
if ($entity instanceof \Cake\Datasource\EntityInterface) {
foreach ($fields as $field => $type) {
if ($entity->has($field)) {
$value = $entity->get($field);
$decryptedValue = $this->decrypt($value);
// Convert DB values to PHP values after decrypting them
$entity->set($field, Type::build($type)->toPHP($decryptedValue, $driver));
$entity->clean();
}
}
}
});
};
$query->formatResults($formatter);
}
示例12: _formatAssociationResults
/**
* Adds a formatter function to the passed `$query` if the `$surrogate` query
* declares any other formatter. Since the `$surrogate` query correspond to
* the associated target table, the resulting formatter will be the result of
* applying the surrogate formatters to only the property corresponding to
* such table.
*
* @param \Cake\ORM\Query $query the query that will get the formatter applied to
* @param \Cake\ORM\Query $surrogate the query having formatters for the associated
* target table.
* @param array $options options passed to the method `attachTo`
* @return void
*/
protected function _formatAssociationResults($query, $surrogate, $options)
{
$formatters = $surrogate->formatResults();
if (!$formatters || empty($options['propertyPath'])) {
return;
}
$property = $options['propertyPath'];
$query->formatResults(function ($results) use($formatters, $property) {
$extracted = $results->extract($property)->compile();
foreach ($formatters as $callable) {
$extracted = new ResultSetDecorator($callable($extracted));
}
return $results->insert($property, $extracted);
}, Query::PREPEND);
}
示例13: _formatAssociationResults
/**
* Adds a formatter function to the passed `$query` if the `$surrogate` query
* declares any other formatter. Since the `$surrogate` query correspond to
* the associated target table, the resulting formatter will be the result of
* applying the surrogate formatters to only the property corresponding to
* such table.
*
* @param \Cake\ORM\Query $query the query that will get the formatter applied to
* @param \Cake\ORM\Query $surrogate the query having formatters for the associated
* target table.
* @param array $options options passed to the method `attachTo`
* @return void
*/
protected function _formatAssociationResults($query, $surrogate, $options)
{
$formatters = $surrogate->formatResults();
if (!$formatters || empty($options['propertyPath'])) {
return;
}
$property = $options['propertyPath'];
$propertyPath = explode('.', $property);
$query->formatResults(function ($results) use($formatters, $property, $propertyPath) {
$extracted = [];
foreach ($results as $result) {
foreach ($propertyPath as $propertyPathItem) {
if (!isset($result[$propertyPathItem])) {
$result = null;
break;
}
$result = $result[$propertyPathItem];
}
$extracted[] = $result;
}
$extracted = new Collection($extracted);
foreach ($formatters as $callable) {
$extracted = new ResultSetDecorator($callable($extracted));
}
return $results->insert($property, $extracted);
}, Query::PREPEND);
}
示例14: findHashed
/**
* Custom finder for hashids field.
*
* Options:
* - hid (required), best to use HashidBehavior::HID constant
* - noFirst (optional, to leave the query open for adjustments, no first() called)
*
* @param \Cake\ORM\Query $query Query.
* @param array $options Array of options as described above
* @return \Cake\ORM\Query
*/
public function findHashed(Query $query, array $options)
{
$field = $this->_config['field'];
if (!$field) {
return $query;
}
$idField = $this->_primaryKey;
$query->formatResults(function ($results) use($field, $idField) {
$newResult = [];
$results->each(function ($row, $key) use($field, $idField, &$newResult) {
if (!empty($row[$idField])) {
$row[$field] = $this->encodeId($row[$idField]);
if ($row instanceof Entity) {
$row->dirty($field, false);
}
$newResult[] = $row;
} elseif (is_string($row)) {
$newResult[$this->encodeId($key)] = $row;
}
});
return new Collection($newResult);
});
if (!empty($options[static::HID])) {
$id = $this->decodeHashid($options[static::HID]);
$query->where([$idField => $id]);
}
$first = $this->_config['findFirst'] === true ? 'first' : $this->_config['findFirst'];
if (!$first || !empty($options['noFirst'])) {
return $query;
}
return $query->first();
}
示例15: findThreaded
/**
* Results for this finder will be a nested array, and is appropriate if you want
* to use the parent_id field of your model data to build nested results.
*
* Values belonging to a parent row based on their parent_id value will be
* recursively nested inside the parent row values using the `children` property
*
* You can customize what fields are used for nesting results, by default the
* primary key and the `parent_id` fields are used. If you you wish to change
* these defaults you need to provide the keys `idField` or `parentField` in
* `$options`:
*
* {{{
* $table->find('threaded', [
* 'idField' => 'id',
* 'parentField' => 'ancestor_id'
* ]);
* }}}
*
* @param \Cake\ORM\Query $query
* @param array $options
* @return \Cake\ORM\Query
*/
public function findThreaded(Query $query, array $options)
{
$options += ['idField' => $this->primaryKey(), 'parentField' => 'parent_id'];
$options = $this->_setFieldMatchers($options, ['idField', 'parentField']);
return $query->formatResults(function ($results) use($options) {
return $results->nest($options['idField'], $options['parentField']);
});
}