随笔 - 23  文章 - 2  trackbacks - 0
<2009年2月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
1234567

留言簿(1)

随笔档案

文章分类

文章档案

最新随笔

搜索

  •  

最新随笔

最新评论

阅读排行榜

评论排行榜

刚才看了一下cake的手册,findAll已经被废弃,所以想知道一下find的用法。我是一个cake的初学者,没有经历过1.1的时代。就直接开始用1.2版本了。不过网上的例子大多数还是老版本1.1的。像html里的东西。现在好多东西已经没有了。相对分离出form,可惜官方档代码太少.再加上英文水平有限.呵呵.看起来好累.

find方法.find($type, $params)
其中$type可以是'all', 'first', 'count', 'neighbors', 'list', 'threaded', 默认用first。

$params是一个下面所列出的选项作为key的数组:

1array(
2    'conditions' => array('Model.field' => $thisValue), //条件数组
3    'recursive' => 1, //整型
4    'fields' => array('Model.field1', 'Model.field2'), //字段名数组
5    'order' => 'Model.created', //定义顺序的字符串或者数组
6    'group' => array('Model.field'), //进行GROUP BY的字段
7    'limit' => n, //整型
8    'page' => n //整型
9)

如果使用find('list')进行查找,$params中的'fields'的值有所不同,这个时候定义的是结果集的索引(key),值(value)和组(group):

1// 生成由Post.id索引的,值为Post.title的list。
2$this->Post->find('list', array('fields'=>'Post.title'));
3 
4// 生成由Post.slug索引的,值为Post.title的list。
5$this->Post->find('list', array('fields'=>array('Post.slug', 'Post.title')));
6 
7// 生成的list将按照Post.author_id进行分组,然后由Post.id进行索引,值为Post.title
8$this->Post->find('list', array('fields'=>array('Post.id', 'Post.title', 'Post.author_id')));
9


如果使用find('neighbors')进行查找,$params中的'field'表示用来分析的字段,'value'则用来定义查找相邻数据(neighbor)时使用的值。注意'field''value'只在find('neighbors')时有效,在find('all’)中是不使用的。

1// 比如有1-10的id,想要取得上一个(1)和下一个(3)的数据。
2$this->Post->id = 2;
3$one = $this->Post->find('neighbors');
4// 使用不同的字段查找相邻数据。
5$two = $this->Post->find('neighbors', array('field'=>'Post.title', 'value'=>$data['Post']['title']));
6

 为了提供对以前版本的兼容,find也可以接收下面的参数形式:

find(string $conditions, array $fields, string $order, int $recursive) 

posted on 2009-02-17 09:53 冰之焰 阅读(7046) 评论(0)  编辑 收藏 引用 网摘

只有注册用户登录后才能发表评论。
网站导航: