Cake PHP 3 needs limit option for find all method

拟墨画扇 提交于 2019-12-11 10:16:26

问题


Inside a cell I need to access the TreeOptions model. So I've wrote this :

    $this->loadModel( 'TreeOptions' );

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

But when I do the foreach like this :

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

It only returns the last record of the result. The only way I've found to make it work as desired is adding the limit clause :

     $i = $this->TreeOptions->find( 'all', [ 'limit' => 200 ] );

And then, I can get the whole set of records. What am I missing ?

Thanks. Regards.


回答1:


In your first snippet, the variable $i, is a state where the query has not yet run. See the excerpt from 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();


来源:https://stackoverflow.com/questions/36086601/cake-php-3-needs-limit-option-for-find-all-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!