【问题标题】:SQL Builder for PHP, with JOIN support?用于 PHP 的 SQL Builder,是否支持 JOIN?
【发布时间】:2010-09-17 23:38:32
【问题描述】:

你们是否知道有一个库可以帮助您构建/操作 SQL 查询并支持 JOIN?

如果你有一些东西可以返回一个对象,它有一些查询集,并且仍然能够对其应用 JOIN、子查询等,它会提供很大的灵活性。

我四处搜索,只找到了SQL Builder,它看起来很基础,不支持连接。这将是一个真正有用的主要功能。

【问题讨论】:

  • 您需要多少功能? SQL的OO抽象?一个ORM?一个“智能”查询构建器,您可以处理字段列表和条件列表,并让您“神奇地”为您构建查询?

标签: php sql join builder sqlbuilder


【解决方案1】:

如果您已经在使用 PDO,FluentPDO 看起来不错:https://github.com/envms/fluentpdo

【讨论】:

    【解决方案2】:

    试试 magsql https://github.com/maghead/magsql,这是一个用 PHP 编写的为提高性能而设计的 SQL 构建器,它提供连接支持和跨平台 SQL 生成。

    目前用在最快的纯PHP orm "maghead"

    【讨论】:

      【解决方案3】:

      你可以使用 lenkorm 这很简单:

      select('contents)->left('categories ON categories.category.id = contents.category_id)->where('content_id = 1')->result();

      或者你可以用作:

      select('contents)->left('categories->using(categoru_id)->where('content_id = 1')->result();

      Download it from github

      【讨论】:

        【解决方案4】:

        我使用 phptoolcase 库中的查询生成器,它使用 pdo 连接器,支持连接。

        http://phptoolcase.com/guides/ptc-qb-guide.html

        您可以将它与库中的连接管理器一起使用,以非常快速地设置多个数据库连接。

        【讨论】:

          【解决方案5】:

          这似乎是一个支持复杂连接的 SQL 构建器:http://laravel.com/docs/queries

          【讨论】:

            【解决方案6】:

            来自 PastaPHP 的基于 SQLObject 的超快 IteratorQuery
            遍历资源

             foreach(_from('users u')
               ->columns("up.email_address AS EmailAddress", "u.user_name AS u.UserName")
               ->left('userprofiles up', _eq('u.id', _var('up.id')))
               ->where(_and()->add(_eq('u.is_active',1)))
               ->limit(0,10)
               ->order("UserName")
               ->execute("myConnection") as $user){
            
               echo sprintf(
                      '<a href="mailto:%s">%s</a><br/>', 
                      $user->EmailAdress, 
                      $user->UserName
               );
             }
            

            【讨论】:

            • 你有可以看到这个库的参考链接吗?
            【解决方案7】:

            Zend_DbZend Framework 包中的Zend_Db_Select 可以执行以下操作:

            // Build this query:
            //   SELECT p."product_id", p."product_name", l.*
            //   FROM "products" AS p JOIN "line_items" AS l
            //     ON p.product_id = l.product_id
            $select = $db->select()
                ->from(array('p' => 'products'), array('product_id', 'product_name'))
                ->join(array('l' => 'line_items'), 'p.product_id = l.product_id');
            

            (来自 Zend 框架手册中的 Example 11.54. Example of the join() method

            如果您不喜欢运行成熟的 ORM 包,这可能是您要走的路。

            【讨论】:

              【解决方案8】:

              我强烈推荐 CakePHP。它会根据表之间的关联自动为您创建连接。

              说如果你正在写博客:

              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:
              
              <h2><?php echo $post['Post']['title']?></h2>
              <p><?php echo $post['Post']['body']; /* Might want Textile/Markdown here */ ?></p>
              <h3>Comments</h3>
              <?php foreach($post['Comment'] as $comment) { ?>
                <p><?php echo $comment['body']?></p>
                <p class="poster"><?php echo $comment['name']?></p>
              <?php } ?>
              

              这就是您查看博客文章所需的全部内容,您的数据库架构已被读取和缓存。只要你保持它与约定一致,你就不必告诉蛋糕任何关于你的桌子是如何设置的。

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

              它具有支持 MySQL、MSSQL、PostgreSQL、SQLite、Oracle 等的适配器。您还可以将 web 服务包装为模型,甚至让它在数据库中的数据和远程数据之间进行连接!这是非常聪明的东西。

              希望这会有所帮助:)

              【讨论】:

                【解决方案9】:

                我建议使用 PHP 框架,例如 Symfony,它默认使用 Propel,如果您愿意,也可以使用 Doctrine。

                CakePHP 也使用了 ORM,但我不知道是哪一个。

                【讨论】:

                  【解决方案10】:

                  也许你可以试试ORM,比如PropelDoctrine,它们有很好的编程查询语言,它们会返回代表数据库中行的对象数组...

                  例如,您可以使用 Doctrine 进行这样的连接:

                  $q = Doctrine_Query::create();
                  $q->from('User u')
                  ->leftJoin('u.Group g')
                  ->innerJoin('u.Phonenumber p WITH u.id > 3')
                  ->leftJoin('u.Email e');
                  
                  $users = $q->execute();
                  

                  还有 Propel:

                  $c = new Criteria(AuthorPeer::DATABASE_NAME);
                  
                  $c->addJoin(AuthorPeer::ID, BookPeer::AUTHOR_ID, Criteria::INNER_JOIN);
                  $c->addJoin(BookPeer::PUBLISHER_ID, PublisherPeer::ID, Criteria::INNER_JOIN);
                  $c->add(PublisherPeer::NAME, 'Some Name');
                  
                  $authors = AuthorPeer::doSelect($c);
                  

                  你可以用这两种方法做更多的事情......

                  【讨论】:

                  • 好吧。很多人喜欢它。但是使用专有的类似 sql 的语言来代替 sql 对我来说似乎并不合适。
                  • @Stanni 我同意你的观点,考虑到 SQL 比专有 sql 语言更复杂
                  猜你喜欢
                  • 2021-08-07
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-10-04
                  • 1970-01-01
                  • 1970-01-01
                  • 2022-01-12
                  • 2017-04-30
                  相关资源
                  最近更新 更多