批判下cakephp中的SQL写法

PHP的框架是超多了,但有时,感觉有些框架是不是考虑的太过度了,总要把简单的事
复杂化,比如下面举出CAKEPHP中的FIND的写法,看上去象HIBERNATE的查询语法,但HIBERNATE的比它简单多了,明明SQL简单通用的东西,要故弄玄虚的搞一翻,还要安上自己的一些规则,所以嘛,不要迷信框架,要选合适自己用的部分,下面举例之:

1 如果我们想找一个ISBN书号为'1234567890'的书,我们只需执行如下SQL命令:
SELECT * FROM `books` AS `Book` WHERE `Book`.`isbn` = '1234567890';
但是在CakePHP的在BooksController中,我们可以像下面这样调用find()方法:

    $this->Book->find('first', array(
                               'conditions' => array(
                               'Book.isbn' => '= 1234567890'
                                                    )
                                    )

    这个还可以接受,再来个:
2 如果我们想查找由'Anupom Syam' , 'Ahsanul Bari' 以及 'David Barnes'等人撰写的所有书籍,我们可以使用下面这个SQL语句来实现:
SELECT * FROM `books` AS `Book` WHERE Book.author_name IN ( 'Anupom Syam', 'Ahsanul Bari', 'David Barnes')
在CakePHP中,我们可以像下面这样使用find()方法来实现上面SQL语句的效果
$this->Book-> find('all', array(
                                 'conditions' => array(
                                   'Book.author_name' => array(
                                                'Anupom Syam',
                                                'Ahsanul Bari',
                                                'David Barnes'
                                                              )
                                                         )
                                    )
                       );我们可以通过在对应的字段名后面设置一个包含有值的数组来实现与SQL逻辑运算符IN()同等的效果。

    你看,已经复杂不少了!

3 如果我们想查找所有由'David Barnes'撰写或者书名中包含有”CakePHP“一词的书,我们只需使用下面这段SQL语句:

    SELECT * FROM `books` AS `Book` WHERE Book.author_name LIKE ( 'David Barnes' OR Book.title LIKE '%CakePHP%')在CakePHP中,它所对应的代码是:
$this->Book-> find('all', array(
                          'conditions' => array(
                                "or" =>
                                array
                                (
                          "Book.author_name" => "David Barnes",
                          "Book.title" => "%CAKEPHP%"
                                )
                             )
                         )
                      );

你看,本来好好的一个SQL语句,还要故弄玄虚的搞成这样,如果不说规则,还真不知道还原成SQL语句是什么样子的,可读性差(把OR单独抽取了出来)

   

你可能感兴趣的:(sql,PHP,Hibernate,框架,cakephp)