在蛋糕PHP 2.x中拥有和属于许多


hasAndBelongsToMany in CakePHP 2.x

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

客户.php(型号)

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

注.php(型号)

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

CustumersController.php (Controller I make the find)

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

谢谢!

解决方案非常简单。

在查找用途中

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

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