Cake PHP 3需要限制选项以查找所有方法

时间:2016-03-18 14:11:41

标签: cakephp orm find clause

在单元格内部,我需要访问TreeOptions模型。 所以我写了这个:

>>> list1 = [
...     ['a', 'apple'],
...     ['b', 'banana'],
...     ['m', 'mango']
... ]
>>>
>>> list2 = [
...     ['b', 'buffalo'],
...     ['zzzz', 'zzzzegemot, a sleeping behemoth'],
...     ['m', 'mongoose']
... ]
>>>
>>> d1, d2 = dict(list1), dict(list2)
>>> common_keys = set(d1) & set(d2)  # => {'b', 'm'}  keys that appears in both dicts.
>>> [[d1[k], d2[k]] for k in common_keys]
[['banana', 'buffalo'], ['mango', 'mongoose']]

但是当我按照这样做时,

    $this->loadModel( 'TreeOptions' );

    $i = $this->TreeOptions->find( 'all' );

它只返回结果的最后一条记录。 我发现使其按预期工作的唯一方法是添加限制条款:

    foreach( $i as $row )
      debug( $row->description );

然后,我可以获得整套记录。 我错过了什么?

感谢。 问候。

1 个答案:

答案 0 :(得分:2)

在您的第一个代码段中,变量$i是查询尚未运行的状态。请参阅CakePHP 3 Cookbook: Retrieving Data & Results — Using Finders to Load Data的摘录:

// Find all the articles.
// At this point the query has not run.
$query = $articles->find('all');

// Iteration will execute the query.
foreach ($query as $row) {
}

// Calling all() will execute the query
// and return the result set.
$results = $query->all();

// Once we have a result set we can get all the rows
$data = $results->toArray();

// Converting the query to an array will execute it.
$results = $query->toArray();
相关问题