cakephp数据库操作find()与query()使用笔记

一:find函数是所有模型都具备的一个获取数据的多功能函数,使用语法为:find($type, $params)。$type可以是'all''first''count''list''neighbors'或者'threaded'的其中之一。默认情况下会使用'first'进行查找,即会查出表的第一条符合的数据;本人觉得find("all",条件)或者findByUsername("username名字")/findById(id号)这几种用起来比较顺手,实用点。

实例:

首先我有一张users表和posts表如下:

CREATE TABLE `users` (
  `id` int(10) NOT NULL auto_increment,
  `username` varchar(50) collate utf8_unicode_ci NOT NULL,
  `password` varchar(50) collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6 ;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `username`, `password`) VALUES
(1, 'admin', '4a7d1ed414474e4033ac29ccb8653d9b'),
(2, 'lht', '4a7d1ed414474e4033ac29ccb8653d9b'),
(3, 'test', 'ca5e6aa29fa7ef71e2655b7f797b98e5');

CREATE TABLE `posts` (
  `id` int(11) NOT NULL auto_increment,
  `user_id` int(11) NOT NULL,
  `title` varchar(255) collate utf8_unicode_ci NOT NULL,
  `body` text collate utf8_unicode_ci,
  `created` datetime default NULL,
  `modified` datetime default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=7 ;

--
-- Dumping data for table `posts`
--

INSERT INTO `posts` (`id`, `user_id`, `title`, `body`, `created`, `modified`) VALUES
(2, 1, 'admin''s message', 'Hi,I''m admin.', '2011-06-21 02:06:40', '2011-06-21 02:06:40'),
(3, 3, 'test', 'Just for a test.', '2011-06-21 02:07:44', '2011-06-21 02:07:44'),
(4, 3, 'user1''s title', 'Just for test.', '2011-06-21 02:13:32', '2011-06-21 02:13:32'),
(5, 1, 'user3''title', 'test', '2011-06-21 02:18:39', '2011-06-21 02:18:39'),
(6, 1, '测试', '测试', '2011-06-22 08:43:06', '2011-06-22 08:43:06');

然后我在users控制器的方法test中执行如下语句:
$result = $this->User->find("all",array('id','username','password'));

$this->set("result",$result);

再在test.ctp视图中打印print_r($result ),将可以看到查询数据表的信息如下:

Array
(
    [0] => Array
        (
            [User] => Array
                (
                    [id] => 1
                    [username] => admin
                    [password] => 4a7d1ed414474e4033ac29ccb8653d9b
                )

        )

    [1] => Array
        (
            [User] => Array
                (
                    [id] => 2
                    [username] => lht
                    [password] => 4a7d1ed414474e4033ac29ccb8653d9b
                )

        )

    [2] => Array
        (
            [User] => Array
                (
                    [id] => 3
                    [username] => test
                    [password] => ca5e6aa29fa7ef71e2655b7f797b98e5
                )

        )

)

上面所述是查询数据库表所有数据的方法,假如我要查询字段username是admin的,那么我可以用findByUsername(字段名)来查询,如下:

$result = $this->User->findByUsername("admin");然后再打印时将只有username是admin的数据显示,效果如下:

Array
(
    [User] => Array
        (
            [id] => 1
            [username] => admin
            [password] => 4a7d1ed414474e4033ac29ccb8653d9b
        )

)

查询admin上面那条语句也可以这样写:$result = $this->User->find('all',array('conditions'=>array('username'=>'admin'))),这样也能达到一样的效果,不过我觉得还是findbyusrname()好用点;

假如我们想查询的结果是字段username不是admin的,那要怎么写呢?很简单,需要用到“<>”这个符号,记得该符号前面要留空格,语句如下:

$result = $this->User->find("all",array('conditions'=>array('username <>'=>'admin')));

此时打印时admin的那一条数据将不显示,其余都显示:

Array
(
    [0] => Array
        (
            [User] => Array
                (
                    [id] => 2
                    [username] => lht
                    [password] => 4a7d1ed414474e4033ac29ccb8653d9b
                )

        )

    [1] => Array
        (
            [User] => Array
                (
                    [id] => 3
                    [username] => test
                    [password] => ca5e6aa29fa7ef71e2655b7f797b98e5
                )

        )

)

 

 二:query()函数:

query()函数被设计用来执行自定义的SQL语句,语法query("A"),A即正常使用的sql语句。在query()执行的返回数据中,使用数据表名作为key而非模型(model)名。

实例:

1:向users表中插入一条数据:

$pass = md5(0000);

$this->User->query("insert into `mycake`.`users` (`id`,`username`,`password`) values(null,'adam_li','$pass')");

2:将字段username为admm_li的语句密码更改为0000:

$this->User->query("update `mycake`.`users` set `password`='0000' where `users`.`username`='adam_li'");

3:删除字段username为admm_li的语句:

$this->User->query("delete from `mycake`.`users` where `users`.`username`='adam_li'");

4:用联合查询查出users表与posts表id相同,结果要users表中的所有内容,posts表的body字段:

在test方法中执行:

$this->set("select",$this->User->query("select `mycake`.`users`.*,`mycake`.`posts`.`body` from `users` left join `posts` on `users`.`id`=`posts`.`id`"));

在test.ctp视图中打印$select:<pre><?php print_r($select); ?></pre>,将显示如下:

Array
(
    [0] => Array
        (
            [users] => Array
                (
                    [id] => 1
                    [username] => admin
                    [password] => 4a7d1ed414474e4033ac29ccb8653d9b
                )

            [posts] => Array
                (
                    [body] => 
                )

        )

    [1] => Array
        (
            [users] => Array
                (
                    [id] => 2
                    [username] => lht
                    [password] => 4a7d1ed414474e4033ac29ccb8653d9b
                )

            [posts] => Array
                (
                    [body] => Hi,I'm admin.
                )

        )

    [2] => Array
        (
            [users] => Array
                (
                    [id] => 3
                    [username] => test
                    [password] => ca5e6aa29fa7ef71e2655b7f797b98e5
                )

            [posts] => Array
                (
                    [body] => Just for a test.
                )

        )

)

注意users表的键值显示为users不是模型User。

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(cakephp数据库操作find()与query()使用笔记)