代码之家  ›  专栏  ›  技术社区  ›  bancer

Cakephp中的并集语法

  •  11
  • bancer  · 技术社区  · 14 年前

    有人知道用CakePHP进行联合查询的好方法吗?我想避免使用 $this->query(); .

    有两个表t1、t2:

    SELECT * FROM t1
    LEFT JOIN t2 ON t1.id = t2.id
    UNION
    SELECT * FROM t1
    RIGHT JOIN t2 ON t1.id = t2.id
    

    SELECT * FROM t1
    LEFT JOIN t2 ON t1.id = t2.id
    LEFT JOIN t3 ON t2.id = t3.id
    UNION
    SELECT * FROM t1
    RIGHT JOIN t2 ON t1.id = t2.id
    LEFT JOIN t3 ON t2.id = t3.id
    UNION
    SELECT * FROM t1
    RIGHT JOIN t2 ON t1.id = t2.id
    RIGHT JOIN t3 ON t2.id = t3.id
    
    4 回复  |  直到 14 年前
        1
  •  13
  •   caitlin    9 年前

    太多的程序员试图将自己局限于框架的功能。不要。使用框架提供的。如果它没有您想要的功能,那么:

    • 将所需的功能编码到类扩展中

    • 自定义在框架内旋转代码以满足您的需要。

    Complex Find Conditions 找到了你的答案:

    $joins = array(
        array(
            'table' => 'test_twos',
            'alias' => 'TestTwo',
            'type' => 'LEFT',
            'conditions' => array(
                'TestTwo.id = TestOne.id',
            )
        ),
        array(
            'table' => 'test_threes',
            'alias' => 'TestThree',
            'type' => 'LEFT',
            'conditions' => array(
            'TestThree.id = TestOne.id',
        )
        )
    );
    
    $dbo = $this->getDataSource();
    $subQuery = $dbo->buildStatement(
        array(
            'fields' => array('*'),
            'table' => $dbo->fullTableName($this),
            'alias' => 'TestOne',
            'limit' => null,
            'offset' => null,
            'joins' => $joins,
            'conditions' => null,
            'order' => null,
            'group' => null
        ),
        $this->TestOne
    );
    $query = $subQuery;
    
    $query .= ' UNION ';
    $joins = array(
        array(
            'table' => 'test_twos',
            'alias' => 'TestTwo',
            'type' => 'LEFT',
            'conditions' => array(
                'TestTwo.id = TestOne.id',
            )
        ),
        array(
            'table' => 'test_threes',
            'alias' => 'TestThree',
            'type' => 'RIGHT',
            'conditions' => array(
            'TestThree.id = TestOne.id',
            )
        )
    );
    
    $dbo = $this->getDataSource();
    $subQuery = $dbo->buildStatement(
        array(
        'fields' => array('*'),
        'table' => $dbo->fullTableName($this),
        'alias' => 'TestOne',
        'limit' => null,
        'offset' => null,
        'joins' => $joins,
        'conditions' => null,
        'order' => null,
        'group' => null
        ),
        $this->TestOne
    );
    
    $query .= $subQuery;
    
    pr($query);
    
        2
  •  5
  •   Warren Sergent    8 年前

    http://dev.mysql.com/doc/refman/5.6/en/create-view.html . 您还可以使用像HeidiSQL这样的软件来帮助您创建视图。

    你的模型中会有这样的东西:

    class Contenu extends AppModel {
        public $useTable = 'v_contenu';
    

    这允许您仍然使用 find() CakePHP中的方法,非常好。

        3
  •  3
  •   Bohemian    14 年前

    使用视图,然后从中选择:

    create view my_union as
    SELECT * FROM t1
    LEFT JOIN t2 ON t1.id = t2.id
    LEFT JOIN t3 ON t2.id = t3.id
    UNION
    SELECT * FROM t1
    RIGHT JOIN t2 ON t1.id = t2.id
    LEFT JOIN t3 ON t2.id = t3.id
    UNION
    SELECT * FROM t1
    RIGHT JOIN t2 ON t1.id = t2.id
    RIGHT JOIN t3 ON t2.id = t3.id
    

    在代码中:

    select * from my_union
    
        4
  •  1
  •   afsane    7 年前

    使用如下代码:

    $friendsPosts= $this->Posts->find('all')
                    ->contain(['Users', 'Languages', 'PostStates'])
                    ->innerJoinWith('Users.Dusers', function ($q) {
                        return $q->where(['Dusers.id' => $this->Auth->user('id')]);
                    });
    
            $posts= $this->Posts->find('all')
                    ->where(['Posts.post_state_id' => 3])
                    ->contain(['Users', 'Languages', 'PostStates']);
    
        $posts->union($friendsPosts);