本文整理汇总了PHP中Cake\ORM\Query::clause方法的典型用法代码示例。如果您正苦于以下问题:PHP Query::clause方法的具体用法?PHP Query::clause怎么用?PHP Query::clause使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\ORM\Query
的用法示例。
在下文中一共展示了Query::clause方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: beforeFind
public function beforeFind(Event $event, Query $query, $options, $primary)
{
if ($query->clause('limit') == 1) {
return $query;
}
foreach ($this->orderBy() as $field => $ord) {
$f = $this->aliasField($field);
$query->order([$this->aliasField($field) => $ord]);
}
if (!is_array($this->primaryKey())) {
return $query;
}
$query->sql();
// force evaluation of internal state/objects
foreach ($query->clause('join') as $join) {
if (!$this->association($join['table'])) {
continue;
}
$table = TableRegistry::get($join['table']);
$table->alias($join['alias']);
foreach ($table->orderBy() as $field => $ord) {
$query->order([$table->aliasField($field) => $ord]);
}
}
return $query;
}
示例2: beforeFind
public function beforeFind(Event $event, Query $query, \ArrayObject $options, $primary)
{
$order = $query->clause('order');
if ($order === null || !count($order)) {
$query->order(['Viewers.name']);
}
}
示例3: beforeFind
public function beforeFind(Event $event, Query $query, \ArrayObject $options, $primary)
{
$order = $query->clause('order');
if ($order === null || !count($order)) {
$query->order(['Films.released DESC', 'Films.title']);
}
}
示例4: findRandom
/**
* "Random" find method
* @param Query $query Query object
* @param array $options Options
* @return Query Query object
*/
public function findRandom(Query $query, array $options)
{
$query->order('rand()');
if (!$query->clause('limit')) {
$query->limit(1);
}
return $query;
}
示例5: findCached
public function findCached(Query $query, array $options)
{
if ($conditions = $query->clause('where')) {
$query->cache(function ($q) use($conditions) {
return $this->table() . '-' . md5(serialize($conditions));
});
}
return $query;
}
示例6: beforeFind
/**
* Add default order clause to query as necessary.
*
* @param \Cake\Event\Event $event Event
* @param \Cake\ORM\Query $query Query
* @param \ArrayObject $options Options
* @param bool $primary Boolean indicating whether it's primary query.
* @return void
*/
public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary)
{
$orders = $this->_config['orders'];
$args = [$query, $options, $primary];
foreach ($orders as $config) {
if (!empty($config['callback']) && call_user_func_array($config['callback'], $args) || !$query->clause('order')) {
$query->order($config['order']);
break;
}
}
}
示例7: scope
/**
* {@inheritDoc}
*
* Look for virtual columns in query's WHERE clause.
*
* @param \Cake\ORM\Query $query The query to scope
* @param string|null $bundle Consider attributes only for a specific bundle
* @return \Cake\ORM\Query The modified query object
*/
public function scope(Query $query, $bundle = null)
{
$whereClause = $query->clause('where');
if (!$whereClause) {
return $query;
}
$whereClause->traverse(function (&$expression) use($bundle, $query) {
if ($expression instanceof ExpressionInterface) {
$expression = $this->_inspectExpression($expression, $bundle, $query);
}
});
return $query;
}
示例8: beforeFind
public function beforeFind(Event $event, Query $query, $options, $primary)
{
$config = $this->config();
$tableAlias = $query->repository()->alias();
$founded = false;
if ($where = $query->clause('where')) {
$where->iterateParts(function ($w) use($config, $tableAlias, &$founded) {
$field = is_object($w) ? $w->getField() : $w;
if ($field == $tableAlias . '.' . $config['field'] || $field == $config['field']) {
$founded = true;
}
return $w;
});
}
if (!$founded) {
$query->where(['deleted' => 0]);
}
}
示例9: scope
/**
* {@inheritDoc}
*
* Look for virtual columns in query's WHERE clause.
*
* @param \Cake\ORM\Query $query The query to scope
* @param string|null $bundle Consider attributes only for a specific bundle
* @return \Cake\ORM\Query The modified query object
*/
public function scope(Query $query, $bundle = null)
{
$orderClause = $query->clause('order');
if (!$orderClause) {
return $query;
}
$class = new \ReflectionClass($orderClause);
$property = $class->getProperty('_conditions');
$property->setAccessible(true);
$conditions = $property->getValue($orderClause);
foreach ($conditions as $column => $direction) {
if (empty($column) || in_array($column, (array) $this->_table->schema()->columns()) || !in_array($column, $this->_toolbox->getAttributeNames())) {
continue;
}
$conditions['(' . $this->_subQuery($column, $bundle) . ')'] = $direction;
unset($conditions[$column]);
}
$property->setValue($orderClause, $conditions);
return $query;
}
示例10: _calculateColumnMap
/**
* Creates a map of row keys out of the query select clause that can be
* used to hydrate nested result sets more quickly.
*
* @return void
*/
protected function _calculateColumnMap()
{
$map = [];
foreach ($this->_query->clause('select') as $key => $field) {
$key = trim($key, '"`[]');
if (strpos($key, '__') > 0) {
$parts = explode('__', $key, 2);
$map[$parts[0]][$key] = $parts[1];
} else {
$map[$this->_defaultAlias][$key] = $key;
}
}
foreach ($this->_matchingMap as $alias => $assoc) {
if (!isset($map[$alias])) {
continue;
}
$this->_matchingMapColumns[$alias] = $map[$alias];
unset($map[$alias]);
}
$this->_map = $map;
}
示例11: _traverseClause
/**
* Traverse over a clause to alias fields
*
* The objective here is to transparently prevent ambiguous field errors by
* prefixing fields with the appropriate table alias. This method currently
* expects to receive a where clause only.
*
* @param \Cake\ORM\Query $query the query to check
* @param string $name The clause name
* @param array $config the config to use for adding fields
* @return bool Whether a join to the translation table is required
*/
protected function _traverseClause(Query $query, $name = '', $config = [])
{
$clause = $query->clause($name);
if (!$clause || !$clause->count()) {
return false;
}
$alias = $config['hasOneAlias'];
$fields = $this->_translationFields();
$mainTableAlias = $config['mainTableAlias'];
$mainTableFields = $this->_mainFields();
$alias = $config['referenceName'];
$joinRequired = false;
$clause->traverse(function ($expression) use($fields, $alias, $mainTableAlias, $mainTableFields, &$joinRequired) {
if (!$expression instanceof FieldInterface) {
return;
}
$field = $expression->getField();
if (!$field || strpos($field, '.')) {
return;
}
if (in_array($field, $fields)) {
$joinRequired = true;
$expression->setField("{$alias}.{$field}");
return;
}
if (in_array($field, $mainTableFields)) {
$expression->setField("{$mainTableAlias}.{$field}");
}
});
return $joinRequired;
}
示例12: beforeFind
/**
* Adds order value if not already set in query.
*
* @param \Cake\Event\Event $event The beforeFind event that was fired.
* @param \Cake\ORM\Query $query The query object.
* @param \ArrayObject $options The options passed to the find method.
*
* @return void
*/
public function beforeFind(Event $event, Query $query, ArrayObject $options)
{
if (!$query->clause('order')) {
$query->order([$this->_table->alias() . '.' . $this->_config['order'] => 'ASC']);
}
}
示例13: testContainToFieldsDefault
/**
* Tests that default fields for associations are added to the select clause when
* none is specified
*
* @return void
*/
public function testContainToFieldsDefault()
{
$contains = ['clients' => ['orders']];
$query = new Query($this->connection, $this->table);
$query->select()->contain($contains)->sql();
$select = $query->clause('select');
$expected = ['foo__id' => 'foo.id', 'clients__name' => 'clients.name', 'clients__id' => 'clients.id', 'clients__phone' => 'clients.phone', 'orders__id' => 'orders.id', 'orders__total' => 'orders.total', 'orders__placed' => 'orders.placed'];
$expected = $this->_quoteArray($expected);
$this->assertEquals($expected, $select);
$contains['clients']['fields'] = ['name'];
$query = new Query($this->connection, $this->table);
$query->select('foo.id')->contain($contains)->sql();
$select = $query->clause('select');
$expected = ['foo__id' => 'foo.id', 'clients__name' => 'clients.name'];
$expected = $this->_quoteArray($expected);
$this->assertEquals($expected, $select);
$contains['clients']['fields'] = [];
$contains['clients']['orders']['fields'] = false;
$query = new Query($this->connection, $this->table);
$query->select()->contain($contains)->sql();
$select = $query->clause('select');
$expected = ['foo__id' => 'foo.id', 'clients__id' => 'clients.id', 'clients__name' => 'clients.name', 'clients__phone' => 'clients.phone'];
$expected = $this->_quoteArray($expected);
$this->assertEquals($expected, $select);
}
示例14: getVirtualColumns
/**
* Gets a list of all virtual columns present in given $query's SELECT clause.
*
* This method will alter the given Query object removing any virtual column
* present in its SELECT clause in order to avoid incorrect SQL statements.
* Selected virtual columns should be fetched after query is executed using
* mapReduce or similar.
*
* @param \Cake\ORM\Query $query The query object to be scoped
* @param string|null $bundle Consider attributes only for a specific bundle
* @return array List of virtual columns names
*/
public function getVirtualColumns(Query $query, $bundle = null)
{
static $selectedVirtual = [];
$cacheKey = md5($query->sql()) . '_' . $bundle;
if (isset($selectedVirtual[$cacheKey])) {
return $selectedVirtual[$cacheKey];
}
$selectClause = (array) $query->clause('select');
if (empty($selectClause)) {
$selectedVirtual[$cacheKey] = array_keys($this->_toolbox->attributes($bundle));
return $selectedVirtual[$cacheKey];
}
$selectedVirtual[$cacheKey] = [];
$virtualColumns = array_keys($this->_toolbox->attributes($bundle));
foreach ($selectClause as $index => $column) {
list($table, $column) = pluginSplit($column);
if ((empty($table) || $table == $this->_table->alias()) && in_array($column, $virtualColumns)) {
$selectedVirtual[$cacheKey][$index] = $column;
unset($selectClause[$index]);
}
}
if (empty($selectClause) && !empty($selectedVirtual[$cacheKey])) {
$selectClause[] = $this->_table->primaryKey();
}
$query->select($selectClause, true);
return $selectedVirtual[$cacheKey];
}
示例15: _appendFields
/**
* Helper function used to conditionally append fields to the select clause of
* a query from the fields found in another query object.
*
* @param \Cake\ORM\Query $query the query that will get the fields appended to
* @param \Cake\ORM\Query $surrogate the query having the fields to be copied from
* @param array $options options passed to the method `attachTo`
* @return void
*/
protected function _appendFields($query, $surrogate, $options)
{
$fields = $surrogate->clause('select') ?: $options['fields'];
$target = $this->_targetTable;
$autoFields = $surrogate->autoFields();
if ($query->eagerLoader()->autoFields() === false) {
return;
}
if (empty($fields) && !$autoFields) {
if ($options['includeFields'] && ($fields === null || $fields !== false)) {
$fields = $target->schema()->columns();
}
}
if ($autoFields === true) {
$fields = array_merge((array) $fields, $target->schema()->columns());
}
if (!empty($fields)) {
$query->select($query->aliasFields($fields, $target->alias()));
}
}