如何改变在CakePHP的加入“序列? [英] How to change the sequence of 'joins' in CakePHP?

查看:131
本文介绍了如何改变在CakePHP的加入“序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有加入的顺序问题。类似的问题是另外一个问题<一href=\"http://stackoverflow.com/questions/3343348/manipulating-order-of-joins-in-cakephp\">http://stackoverflow.com/questions/3343348/manipulating-order-of-joins-in-cakephp.答案是使用容纳的行为。在我的情况是不可接受的,因为我有更深的关联和容纳的产生太多的疑问。容纳的不产生连接的三个电平关联。它从第二级表的每个条目生成额外的查询。

I have the problem with the sequence of joins. The similar problem was in another question http://stackoverflow.com/questions/3343348/manipulating-order-of-joins-in-cakephp. The answer was to use Containable behavior. In my case that is unacceptable because I have deeper associations and containable generates too many queries. Containable does not generate joins for the three level associations. It generates additional queries for every entry from the second level table.

我的查询是:

$this->LevelOne->find('all', array(
    'joins' => array(array(
         'table' => 'level_three',
         'alias' => 'LevelThree',
         'type' => 'LEFT',
         'conditions' => array(
              'LevelThree.id = LevelTwo.level_three_field_id'
          )
     ))
));

这里的问题是,蛋糕生成若干个连接,但LevelThree表的加入LevelTwo表的联接和抛出一个SQL错误,在关于条款'未知列'LevelTwo.level_three_field_id'之前完成。如果LevelThree连接就可以在查询结束后,所有LevelTwo加入查询会好起来的。

The problem here is that cake generates several joins but the join of the LevelThree table is done before the joins of the LevelTwo tables and that throws an SQL error "Unknown column 'LevelTwo.level_three_field_id' in 'on clause'". If the LevelThree join would be at the end of the query after all LevelTwo joins the query would be okay.

所以,问题是如何改变连接的顺序?

So, the question is how to change the sequence of joins?

推荐答案

最后我想通了,如何做到这一点:

Finally I figured out how to do that:

$this->LevelOne->unbindModel(array('belongsTo' => array('LevelTwo')));
$this->LevelOne->find('all', array(
    'joins' => array(
          array(
             'table' => 'level_two',
             'alias' => 'LevelTwo',
             'type' => 'LEFT',
             'conditions' => array(
                  'LevelTwo.id = LevelOne.level_two_field_id'
              )
          ),
          array(
             'table' => 'level_three',
             'alias' => 'LevelThree',
             'type' => 'LEFT',
             'conditions' => array(
                  'LevelThree.id = LevelTwo.level_three_field_id'
              )
          )
     )
));

这篇关于如何改变在CakePHP的加入“序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆