cakephp數據庫操做find()與query()使用筆記

一:find函數是全部模型都具有的一個獲取數據的多功能函數,使用語法爲:find($type, $params)。$type能夠是'all''first''count''list''neighbors'或者'threaded'的其中之一。默認狀況下會使用'first'進行查找,即會查出表的第一條符合的數據;本人以爲find("all",條件)或者findByUsername("username名字")/findById(id號)這幾種用起來比較順手,實用點。php

實例:sql

首先我有一張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');post

而後我在users控制器的方法test中執行以下語句:
$result = $this->User->find("all",array('id','username','password'));測試

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

再在test.ctp視圖中打印print_r($result ),將能夠看到查詢數據表的信息以下:spa

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的數據顯示,效果以下:3d

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。

相關文章
相關標籤/搜索