CakePHP3需要查找所有方法的限制选项 [英] Cake PHP 3 needs limit option for find all method

查看:239
本文介绍了CakePHP3需要查找所有方法的限制选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单元格内部,我需要访问TreeOptions模型。
所以我写了这个:
$ b $ pre $ $ this-> loadModel('TreeOptions');

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

但是当我做这样的foreach:

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

它只返回结果的最后一个记录。
我找到的唯一方法是按照需要添加限制条款:

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

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



谢谢。
关心。

解决方案

在你的第一个片段中,变量 $ i 是查询尚未运行的状态。见 CakePHP 3 Cookbook:Retrieving Data&结果 - 使用Finders加载数据

  //找到所有文章。 
//此时查询没有运行。
$ query = $ articles-> find('all');

//迭代将执行查询。
foreach($ query as $ row){
}

//调用all()会执行查询
//并返回结果集。
$ results = $ query-> all();

//一旦我们有结果集,我们可以得到所有的行
$ data = $ results-> toArray();

//将查询转换为数组将执行它。
$ results = $ query-> toArray();


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.

解决方案

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();

这篇关于CakePHP3需要查找所有方法的限制选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆