CakePHP查询数据

    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'
    )

  • conditions(条件):条件(conditions)可应用于取代查询语句中的WHERE子句。默认值是1=1,这表示没条件约束。
  • fields(字段):查询结果应该返回的字段范围。如果没有指明,它会返回所有字段。当find()函数的第一个参数$type被设置成count时,该参数不可用。
  • order(排序):这个字符串定义了查询语句中ORDER BY子句的内容。如果order排序没有指明,那么SQL查询中将不会添加有ORDER BY子句。查询类型是count是,该参数也不可用。
  • limit(限制):该整数指明了返回记录的最大数量。如果没指定limit,那么函数会返回所有符合条件的记录。只有当查询类型是all时,它才可用。




$result = $this->User->find("all",array('id','username','password'));

$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!




posted @ 2012-02-16 15:30  夏至冬末  阅读(517)  评论(0编辑  收藏  举报