本文整理汇总了PHP中Cake\ORM\Query::contain方法的典型用法代码示例。如果您正苦于以下问题:PHP Query::contain方法的具体用法?PHP Query::contain怎么用?PHP Query::contain使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\ORM\Query
的用法示例。
在下文中一共展示了Query::contain方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: findMetCustomers
public function findMetCustomers(Query $query, array $options)
{
$query->contain(['Users.Customers'])->matching('Users', function ($q) use($options) {
return $q->where(['Users.id' => $options['Users.id']]);
});
return $query;
}
示例2: findWithContain
public function findWithContain(Query $query, array $options = [])
{
if (!empty($this->_contain)) {
$query->contain($this->_contain);
}
return $query;
}
示例3: beforePaginate
/**
* {@inheritDoc}
*/
public function beforePaginate(Event $event, Query $query)
{
$query->contain($this->_getAssociations($event));
$this->_filterByConditions($query, $event);
$this->_selectActionFields($query, $event);
$this->_handleDtSorting($query, $event);
}
示例4: onBeforeFind
/**
* Before find any users, set contain to get user roles too.
*
* @param Event $event
* @param Query $query
* @param \ArrayObject $options
* @param $primary
*/
public function onBeforeFind(Event $event, $query, \ArrayObject $options, $primary)
{
$table = $event->subject();
if ($table->alias() == 'Users') {
$query->contain(['Roles']);
}
}
示例5: 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;
}
示例6: findParticipationsForStudent
public function findParticipationsForStudent(Query $query, array $options)
{
return $query->contain(['Tests' => function ($q) {
return $q->select(['Tests.datepreuve']);
}, 'Tests.Subjects' => function ($q) {
return $q->select(['Subjects.libelle', 'Subjects.coeff']);
}])->where($options)->hydrate(false);
}
示例7: findTagged
public function findTagged(Query $query, array $options)
{
$query->contain(['Users', 'Users.AccountParameters', 'Hashtags']);
$query->matching('Hashtags', function ($q) use($options) {
return $q->where(['Hashtags.name' => $options['tag_name']]);
});
$query->order(['Tweets.created' => 'DESC']);
return $query;
}
示例8: findToAutoRenew
/**
* Finds all memberships that will expire in the next 24 hours, are marked
* for automatic renewal, and have not been renewed or canceled.
*
* @param Query $query
* @param array $options
* @return Query
*/
public function findToAutoRenew(Query $query, array $options)
{
return $query->contain(['Users' => function ($q) {
return $q->select(['id', 'name', 'email', 'stripe_customer_id']);
}, 'MembershipLevels' => function ($q) {
return $q->select(['id', 'name', 'cost']);
}])->where(function ($exp, $q) {
return $exp->isNull('canceled');
})->where(function ($exp, $q) {
return $exp->lte('expires', date('Y-m-d H:i:s', strtotime('+1 day')));
})->where(['auto_renew' => 1])->order(['expires' => 'ASC']);
}
示例9: getProperty
/**
* Get CakePHP property
*
* @param string $dataName Column data name
*
* @throws Exception
* @return array|string
*/
protected function getProperty($dataName)
{
$dataName = explode('.', trim($dataName));
if (count($dataName) != 2) {
throw new Exception('You are set invalid date.');
}
$tableAlias = $dataName[0];
$colName = $dataName[1];
if ($this->query->repository()->alias() == $tableAlias) {
return $colName;
} elseif (array_key_exists($tableAlias, $this->query->contain())) {
return ['propertyPath' => $this->query->eagerLoader()->normalized($this->query->repository())[$tableAlias]['propertyPath'], 'field' => $colName];
}
}
示例10: findReadStatus
/**
* Dynamic finder that loads all users for a thread without me
*
* @param \Cake\ORM\Query $query the original query to append to
* @param array $users the list of users to be ignored
* @return \Cake\ORM\Query The amended query
*/
public function findReadStatus(Query $query, array $users)
{
$query->contain(['Messages' => function ($q) use($users) {
return $q->find('readStatus', $users);
}]);
return $query->map(function ($thread) {
$opened = false;
foreach ($thread['messages'] as $message) {
if ($message['message_read_statuses'][0]['opened']) {
$opened = true;
break;
}
}
$thread['opened'] = $opened;
return $thread;
});
}
示例11: 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;
});
});
}
}
}
示例12: beforeFind
/**
* [beforeFind description]
*
* @param Event $event [description]
* @param Query $query [description]
* @param array $options
*
* @return $this|array|Query [type] [description]
*
* ### Options
* `images` When setting images to false nothing will be added to the query and no image fields will be returned in the resultset and will probably
* speed up overall performance
*/
public function beforeFind(Event $event, Query $query, $options = [])
{
if (isset($options['images']) && !$options['images']) {
return $query;
}
$fields = $this->config('fields');
$contain = $conditions = [];
foreach ($fields as $field => $type) {
$field = $this->_fieldName($field);
$contain[$field] = $conditions;
}
/**
* @param $row
* @param $key
* @param $mapReduce MapReduce
*/
$mapper = function ($row, $key, $mapReduce) use($fields) {
foreach ($fields as $field => $type) {
$name = $this->_fieldName($field, false);
$image = isset($row[$name]) ? $row[$name] : null;
// make sure we set the correct registry alias for the entity so
// we can access the entity's repository from the ImageHelper
if (!empty($image)) {
if (is_array($image)) {
foreach ($image as &$imageEntity) {
$this->_setEntitySource($imageEntity);
}
} else {
$this->_setEntitySource($image);
}
}
if ($image === null) {
unset($row[$name]);
continue;
}
$row[$field] = $image;
unset($row[$name]);
}
if ($row instanceof Entity) {
$row->clean();
}
$mapReduce->emitIntermediate($row, $key);
};
/**
* @param $items
* @param $key
* @param $mapReduce MapReduce
*/
$reducer = function ($items, $key, $mapReduce) {
if (isset($items[0])) {
$mapReduce->emit($items[0], $key);
}
};
$request = Router::getRequest();
if (!isset($request->params['prefix']) || $request->params['prefix'] !== 'admin') {
//TODO zuniverzálnit
foreach ($contain as $key => &$item) {
$item['conditions'] = [$key . '.active' => true];
}
}
$q = $query->contain($contain)->mapReduce($mapper, $reducer);
return $q;
}
示例13: findOfHired
public function findOfHired(Query $query, $options = [])
{
return $query->contain(['Employees'])->where(['Salaries.to_date IS' => null]);
}
示例14: findWithOfficialLanguage
public function findWithOfficialLanguage(Query $query)
{
return $query->contain('OfficialLanguages');
}
示例15: findLanguage
/**
* Custom finder method
* users translating to/from given language
*
* @param Query $query
* @param array $options ['language_id'] == language_id, ['target'] == true (target language) false (source language)
* @return Query
*/
public function findLanguage(Query $query, array $options)
{
$query->contain(['LanguagesDirection'])->matching('LanguagesDirection', function (\Cake\ORM\Query $query) use($options) {
return $query->where(['LanguagesDirection.language_id' => $options['language_id'], 'LanguagesDirection.target' => $options['target']]);
});
return $query;
}