Cakephp 升级1.3之后->;2.4:find()不';t连接关联模型

Cakephp 升级1.3之后->;2.4:find()不';t连接关联模型,cakephp,join,find,cakephp-2.0,cakephp-1.3,Cakephp,Join,Find,Cakephp 2.0,Cakephp 1.3,我最近用 但是现在$this->Model->find()的一些用法不起作用。尤其是关联模型/表的联接。导致“where子句中的未知列'Bill.customer\u id' 我的设置: 表格和关联:(图) 型号: App::uses('AppModel', 'Model'); class Customer extends AppModel { public $actsAs = array('Logable', 'Containable'); public $hasMany = array(

我最近用
但是现在$this->Model->find()的一些用法不起作用。尤其是关联模型/表的联接。
导致“where子句中的未知列'Bill.customer\u id'

我的设置:

表格和关联:(图)

型号

App::uses('AppModel', 'Model');
class Customer extends AppModel {
public $actsAs = array('Logable', 'Containable');
public $hasMany = array(
    'Bill' => array(
        'className' => 'Bill',
        'foreignKey' => 'customer_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )...
-----------------------------------------------------------------
App::uses('AppModel', 'Model');
class Bill extends AppModel {
public $actsAs = array('Containable', 'Logable', 'Lockable');

public $belongsTo = array(
    'Customer' => array(
        'className' => 'Customer',
        'foreignKey' => 'customer_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )...

public $hasAndBelongsToMany = array(
    'Stage' => array(
        'className' => 'Stage',
        'joinTable' => 'bills_stages',
        'foreignKey' => 'bill_id',
        'associationForeignKey' => 'stage_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )...
--------------------------------------------------------------
App::uses('AppModel', 'Model');
class BillsStage extends AppModel {
public $actsAs = array('Containable', 'Logable', 'Lockable');

public $belongsTo = array(
    'Bill' => array(
        'className' => 'Bill',
        'foreignKey' => 'bill_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Stage' => array(
        'className' => 'Stage',
        'foreignKey' => 'stage_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
)...
--------------------------------------------------------
App::uses('AppModel', 'Model');
class Stage extends AppModel {
public $displayField = 'name';
public $actsAs = array('Tree', 'Containable', 'Logable');

public $hasMany = array(
    'Bill' => array(
        'className' => 'Bill',
        'foreignKey' => 'stage_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )...

public $hasAndBelongsToMany = array(
    'Bill' => array(
        'className' => 'Bill',
        'joinTable' => 'bills_stages',
        'foreignKey' => 'stage_id',
        'associationForeignKey' => 'bill_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )...
在1.3中,连接正在工作。在2.4中,只有一维sql查询

有什么想法吗

--编辑--

尝试联接(从中取出的示例)

(根据您的情况进行调整)


根据,不可能对二级模型上的条件执行
包含
,因为
包含
会进行不同的查询。因此,如果您想在二级模型上应用条件,请使用联接,或者在您的模型中创建一个简单函数,该函数执行两个单独的查询,并创建一个宏数组,该数组返回混合结果,如果您想使用
包含

您的
$this->model->find()
看起来如何?抱歉,现在添加了my find()-调用。请看一看@Ctravel模型的递归值是多少?它们是可包含的吗?我尝试了
$this->Customer->Bill->BillsStage->recursive
的所有值。没有什么变化。所有型号均为可容纳型。在1.3中一切都很好@Nunserset debug level为2,并检查创建错误的完整sql语句。可能只是输入错误。带有Stage.id的“条件”仍然是“未知列”。如果我删除条件->将进行左连接,但结果为160万个条目。整张桌子只有20k。我要面对这个问题两天了,我知道。无法想象为什么它在1.3版上有效而在2.4版上无效。研究了cakephp的所有变更记录。。。这就是为什么我说你需要适应你的情况。在Stage中是另一个表,则需要连接,而不是条件。我更新了。我不知道表的所有定义,所以我假设您有类似于
bill\u stages
bill\u id
stage\u id
,那么为什么不尝试将
stage\u id=>array\u键($vk\u stages)
放在
条件下,看看它是如何运行的……当然,我试过了。但左路连接正在破裂。结果太多了。你们在这里看到的我的表格:我的一般问题不仅仅是这个案例。在我的旧1.3应用程序中有更多这样的应用程序。我想知道有什么变化。修复每一个查找呼叫都会造成伤害。;-)谢谢。嗯,也许我错过了从<代码>账单
到<代码>账单阶段
$billsStages = $this->Customer->Bill->BillsStage->find('all', array(
  'conditions' => array(
      'Bill.customer_id' => $this->Customer->id,
      'Stage.id' => array_keys($vk_stages)
    ),
  'order' => 'BillsStage.created DESC'
));
$options['joins'] = array(
    array('table' => 'bills',
        'alias' => 'Bill',
        'type' => 'LEFT',
        'conditions' => array(
            'Bill.customer_id' => $this->Customer->id,
        )
    ),
    array('table' => 'stages',
        'alias' => 'Stage',
        'type' => 'LEFT',
        'conditions' => array(
            'Stage.id' => array_keys($vk_stages)
        )
    )
);

$options['conditions'] = array();

$items= $this->Customer->Bill->BillsStage->find('all', $options);