array( 'conditions' => array('Model.field' => $thisValue), //条件数组 'recursive' => 1, //整型 'fields' => array('Model.field1', 'DISTINCT Model.field2'), //字段名数组 'order' => array('Model.created', 'Model.field3 DESC'), //定义顺序的字符串或者数组 'group' => array('Model.field'), //进行GROUP BY的字段 'limit' => n, //整型 'page' => n, //整型 'callbacks' => true //other possible values are false, 'before', 'after' )
$this->set("result",$result);
$result = $this->User->findByUsername("admin");
//打印时将只有username是admin的数据显示
$result = $this->User->find("all",array('conditions'=>array('username <>'=>'admin')));
此时打印时admin的那一条数据将不显示,其余都显示
为了便于读者更清楚地了解这个概念,我们将会快速浏览下面这些表明了find()用法的例子:
1,如果你想知道书名以字母“A”开头的书的数目,我们会在BooksController(Books控制器中)添加如下代码:
$count = $this->Book->find('count', array('conditions' =>
array('Book.title' => 'LIKE A%'));
It executes the following SQL query:
它会执行如下SQL查询
SELECT COUNT(*) AS `count` FROM `books` AS `Book` WHERE
`Book`.`title` LIKE 'A%';
当find()方法的$type参数设置成count时,查询返回的结果是一个整数。在这个例子中, $count变量的值可能是2.
2,如果我们想查询id最大书本所对应的书号(ISBN)和书名,我们会使用下面这个代码
$book = $this->Book->find('first',
array(
'fields' => array('isbn', 'title'),
'order' => 'Book.id DESC'
)
);
该代码会执行如下SQL语句
SELECT `Book`.`isbn`, `Book`.`title` FROM `books` AS `Book`
WHERE 1 = 1 ORDER BY `Book`.`created` DESC LIMIT 1;
储存在$book变量中的结果会是下面这个样子:
Array
(
[Book] => Array
(
[isbn] => 1847192971
[title] => Building Powerful and Robust Websites
with Drupal 6
)
)
3,如果你想找出某个作者所写的所得书并按照书名来排序,那么下面这个代码可以达到此目的:
$books = $this->Book->find('all',
array(
'fields' => array('title'),
'conditions' => array(
'Book.author_name' => 'LIKE David Barnes'
),
'order' => 'Book.title ASC'
)
);
上面这段代码将会执行下面这个SQL查询
SELECT `Book`.`title` FROM `books` AS `Book` WHERE `Book`.`author_
name` LIKE 'David Barnes' ORDER BY
`Book`.`title` ASC
上面这段查询会返回如下的结果:
Array
(
[0] => Array
(
[Book] => Array
(
[title] => How to write computer books
)
)
[1] => Array
(
[Book] => Array
(
[title] => How not to write a technical book!