简体   繁体   English

CakePHP 2.x中的hasAndBelongsToMany

[英]hasAndBelongsToMany in CakePHP 2.x

I need help for the simple relationchip in cakephp 2.x, I believe this right however not working: 我需要关于cakephp 2.x中的简单relationchip的帮助,但我相信这种权利不起作用:

Customer.php (Model) Customer.php(模型)

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

Note.php (Model) Note.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) 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);
}

The error 错误

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

Thanks! 谢谢!

The soluction is really simple. 解决方案非常简单。

In find uses 寻找用途

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

Works! 作品! , but now the recursive mode don't work... ,但现在递归模式不起作用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM