CakePHP 2.x中有hasAndBelongsToMany

时间:2016-02-08 11:58:57

标签: php cakephp

我需要帮助cakephp 2.x中的简单关系芯片,我相信这是正常的,但是不起作用:

Customer.php(型号)

public $hasAndBelongsToMany = array(
    'Note' => array(
        'className' => 'Note',
        'joinTable' => 'customers_notes',
        'foreignKey' => 'customer_id',
        'associationForeignKey' => 'note_id',
        'unique' =>false,
        'dependent'=>true,
    )
);

Note.php(模型)

public $hasAndBelongsToMany = array(
    'Customer' => array(
        'className' => 'Customer',
        'joinTable' => 'customers_notes',
        'foreignKey' => 'note_id',
        'associationForeignKey' => 'customer_id',
        'unique' =>false,
        'dependent'=>true
    )
);

CustumersController.php(控制器我查找)

public function admin_notes($id=null){
    $this->layout="Adm.admin";

    $all = $this->Customer->Note->find('all', array(
        'limit' => 10,
        'conditions' => array(
            "CustomersNote.customer_id" => $id
        ),
        'order'=>'CustomersNote.id DESC'
    ));
    $this->set('notes', $all);
}

错误

SQL Query: SELECT `Note`.`id`, `Note`.`titulo`, `Note`.`conteudo`, `Note`.`created`, `Note`.`modified`, `Note`.`user_id` FROM `crm_prado`.`notes` AS `Note` WHERE `CustomersNote`.`customer_id` = '1' LIMIT 10

谢谢!

1 个答案:

答案 0 :(得分:0)

解决方案非常简单。

查找用途

$all = $this->Customer->find('all', array(
        'recursive'=>2,
        'conditions'=>array(
            'Customer.id'=>$id
        )
 ));

工作!,但现在递归模式不起作用......