SQL Builder for PHP, with JOIN support?

后端 未结 10 1886
-上瘾入骨i
-上瘾入骨i 2020-12-19 15:29

Are any of you aware of a library that helps you build/manipulate SQL queries, that supports JOIN\'s?

It would give a lot of flexibility i\'d think if you have somet

10条回答
  •  时光说笑
    2020-12-19 16:03

    I highly recommend CakePHP. It creates joins for you automatically, based on the associations between tables.

    Say if you were writing a blog:

    app/model/post.php:
    
    class Post extends AppModel {
      var $hasMany = array('Comment');
    }
    
    app/controller/posts_controller.php:
    
    function view($id) {
      $this->set('post', $this->Post->read(null, $id));
    }
    
    app/views/posts/view.ctp:
    
    

    Comments

    That would be all you have to write to view a blog post, your database schema is read and cached. As long as you keep it consistent with the conventions, you don't have to tell cake anything about how your table is set up.

    posts:
    id INT
    body TEXT
    created DATETIME
    
    comments:
    id INT
    body TEXT
    name VARCHAR
    post_id INT
    

    It has adapters to support MySQL, MSSQL, PostgreSQL, SQLite, Oracle and others. You can also wrap webservices as models, and even get it to do joins between data in your database and remote data! It's very clever stuff.

    Hope this helps :)

提交回复
热议问题