为了便于读者更清晰地懂得这个概念,咱们将会疾速阅读上面这些标明了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,假如你想找出某个作者所写的所得书并依照书名来排序,那末上面这个代码可以到达此目标: