当前位置:编程学习 > php >>

CakePHP 2.x CookBook 中文版 第七章 模型 之 检索数据

检索数据
如前所述,模型层的一个角色是从多种存储中获取数据。 CakePHP 模型类带有很多功能,帮助你搜索这些数据,排序,分页并且进行过滤。你将要使用的很多功能集成于模型的 Model::find()
 
find
find(string $type = 'first', array $params = array())
 
Find 是所有模型数据检索功能的主力。 $type 可以是 'all', 'first', 'count', 'list', 'neighbors', 'threaded'或者任何自定义查找类型。 切记,$type 是区分大小写的。 使用大写字母(例如 All)将得不到期望的结果。
 
$params 用于向不同的 find 传递所有的参数,其默认有如下的键值 - 每一个都是可选的:
 
 1 array(
 2     'conditions' => array('Model.field' => $thisValue), //array of conditions
 3     'recursive' => 1, //int
 4     'fields' => array('Model.field1', 'DISTINCT Model.field2'), //array of field names
 5     'order' => array('Model.created', 'Model.field3 DESC'), //string or array defining order
 6     'group' => array('Model.field'), //fields to GROUP BY
 7     'limit' => n, //int
 8     'page' => n, //int
 9     'offset' => n, //int
10     'callbacks' => true //other possible values are false, 'before', 'after'
11 )
也可以添加和使用其它的参数,提供给一些查找类型、行为以及你自己的模型方法。
 
find(‘first’)
find('first', $params) 返回一个结果,你可以在任何期望获得一个结果的情况下使用它。 下面是几个简单的(控制器代码)示例:
 
 1 public function some_function() {
 2     // ...
 3     $semiRandomArticle = $this->Article->find('first');
 4     $lastCreated = $this->Article->find('first', array(
 5         'order' => array('Article.created' => 'desc')
 6     ));
 7     $specificallyThisOne = $this->Article->find('first', array(
 8         'conditions' => array('Article.id' => 1)
 9     ));
10     // ...
11 }
在第一个示例中,没有向 find 传递任何参数 - 所以没有任何条件和排序。这种形式的 find('first') 调用返回的格式如下:
 
 1 Array
 2 (
 3     [ModelName] => Array
 4         (
 5             [id] => 83
 6             [field1] => value1
 7             [field2] => value2
 8             [field3] => value3
 9         )
10 
11     [AssociatedModelName] => Array
12         (
13             [id] => 1
14             [field1] => value1
15             [field2] => value2
16             [field3] => value3
17         )
18 )
find(‘count’)
 
find('count', $params) 返回一个整数值。下面是几个简单的(控制器代码)示例:
 
 1 public function some_function() {
 2     // ...
 3     $total = $this->Article->find('count');
 4     $pending = $this->Article->find('count', array(
 5         'conditions' => array('Article.status' => 'pending')
 6     ));
 7     $authors = $this->Article->User->find('count');
 8     $publishedAuthors = $this->Article->find('count', array(
 9        'fields' => 'DISTINCT Article.user_id',
10        'conditions' => array('Article.status !=' => 'pending')
11     ));
12     // ...
13 }
注解
 
不要向 find('count') 传递 fields 数组。你只能为 DISTINCE count 指定列(其它情况下,计数结果总是相同的 - 仅取决于条件)。
 
find(‘all’)
find('all', $params) 返回一个数组(可能有多个)结果。
 
实际上,它是全部 find() 的变体(包括分页)。下面是几个简单的(控制器代码)示例:
 
 1 public function some_function() {
 2     // ...
 3     $allArticles = $this->Article->find('all');
 4     $pending = $this->Article->find('all', array(
 5         'conditions' => array('Article.status' => 'pending')
 6     ));
 7     $allAuthors = $this->Article->User->find('all');
 8     $allPublishedAuthors = $this->Article->User->find('all', array(
 9         'conditions' => array('Article.status !=' => 'pending')
10     ));
11     // ...
12 }
注解
 
上面的例子中, $allAuthors 将包含 users 表的每个用户。没有要应用的条件被传递给那个 find。
 
调用 find('all') 的结果格式如下:
 
 1 Array
 2 (
 3     [0] => Array
 4         (
 5             [ModelName] => Array
 6                 (
 7                     [id] => 83
 8                     [field1] => value1
 9                     [field2] => value2
10                     [field3] => value3
11                 )
12 
13             [AssociatedModelName] => Array
14                 (
15                     [id] => 1
16                     [field1] => value1
17                     [field2] => value2
18                     [field3] => value3
19               &
补充:Web开发 , php ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,