本文整理汇总了PHP中Cake\ORM\Query::first方法的典型用法代码示例。如果您正苦于以下问题:PHP Query::first方法的具体用法?PHP Query::first怎么用?PHP Query::first使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\ORM\Query
的用法示例。
在下文中一共展示了Query::first方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: prepareTableData
/**
* converts a query Object into a flat table
* properties are filed in first row
*
* @param Query $query
* @return array
*/
public function prepareTableData(Query $query = null)
{
/* first row contains properties */
$data = [array_keys($query->first()->toArray())];
/** add data of an entity a row */
foreach ($query as $entity) {
$data[] = array_values($entity->toArray());
}
return $data;
}
示例2: testFirstMapReduce
/**
* Tests that first can be called against a query with a mapReduce
*
* @return void
*/
public function testFirstMapReduce()
{
$map = function ($row, $key, $mapReduce) {
$mapReduce->emitIntermediate($row['id'], 'id');
};
$reduce = function ($values, $key, $mapReduce) {
$mapReduce->emit(array_sum($values));
};
$table = TableRegistry::get('articles', ['table' => 'articles']);
$query = new Query($this->connection, $table);
$query->select(['id'])->hydrate(false)->mapReduce($map, $reduce);
$first = $query->first();
$this->assertEquals(1, $first);
}
示例3: 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();
}