0

我最近使用https://github.com/dereuromark/upgrade 将我的 1.3-app 升级到 2.4
但是现在$this->Model->find() 的某些用法不起作用。尤其是与关联模型/表的连接。
导致“where 子句中的未知列 'Bill.customer_id'”。

我的设置:

表格和关联: http: //i.stack.imgur.com/bjOIz.png (图片)

型号

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 查询。

任何想法?

--编辑--

$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'
));
4

1 回答 1

0

尝试加入(从文档中取出的示例)

$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);

(根据您的情况调整)。

根据这个答案contain辅助模型上的 for 条件是不可能的,因为contain会进行不同的查询。因此,如果您想在辅助模型上应用条件,请使用连接,或者在您的模型中创建一个简单的函数,该函数执行两个单独的查询并创建一个宏数组,如果您想使用返回混合结果的contain.

于 2013-11-15T12:43:58.020 回答