简体   繁体   English

CakePhp:建立递归关联模型并查找

[英]CakePhp: model recursive associations and find

I've some trouble with a find() on a model on CakePhp. 我在CakePhp的模型上使用find()遇到了一些麻烦。

I have three model relationed in this way: 我通过这种方式关联了三个模型:

Project(some_fields, item_id) ------belongsTo-----> Item(some_fields, item_id) ------belongsTo-----> User(some_fields campi, nickname) 项目(some_fields,item_id)------属于----->项目(some_fields,item_id)------属于To ----->用户(some_fields campi,昵称)

I need to do a find() and retrieve all fields from project, a field from Item and the nickname field from User. 我需要执行一次find()并从项目中检索所有字段,从项目中检索一个字段,从用户中检索昵称字段。 This is my code: 这是我的代码:

$this->set('projects', $this->Project->find('all', array('recursive' => 2)));

but my output doesn't contains the user object. 但我的输出不包含用户对象。 I've tried with Containable behaviour but the output is the same. 我尝试了Containable行为,但输出是相同的。 What is broken? 什么坏了?

Many many Thanks 非常感谢

Peter 彼得

Set Your model properly or try with join 正确设置模型或尝试加入

$this->Project->recursive = -1;
$this->Project->find('all', array(
   'joins'=>array(
      array(
        'table'=>'item',
        'alias'=>'Item',
        'type'=>'INNER',
        'conditions'=>array(
           'Item.id=Project.item_id'
             )
          ),
       array(
         'table'=>'user',
         'alias'=>'User',
         'type'=>'INNER',
         'conditions'=>array(
            'User.id=Item.user_id'
         )
      )
   ),
   'fields'=>array(
     //necessary
   ),
   'conditions'=>array(
     //if any
   );

Project.php Model file should be as below Project.php模型文件应如下所示

<?php
App::uses('AppModel','Model');
/*
*
* Project Model
*
*/
Class Project extends AppModel{

/*
*
* actsAs Behaviour
*
*/
    public $actsAs = array('Containable');

/*
*
* belongsTO association
*
*/  
    public $belongsTo = array(
        'Item' => array(
            'className' => 'Item',
            'foreignKey' => 'project_id',
            )
        );
}
?>

Item.php Model file should be as below Item.php模型文件应如下所示

<?php
App::uses('AppModel','Model');
/*
*
* Item Model
*
*/
Class Item extends AppModel{

/*
*
* actsAs Behaviour
*
*/
    public $actsAs = array('Containable');

/*
*
* belongsTO association
*
*/  
    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'item_id',
            )
        );
/*
*
* hasMany association
*
*/  
    public $hasMany = array(
        'Project' => array(
            'className' => 'Project',
            'foreignKey' => 'project_id',
            )
        );  
}
?>

User.php Model file should be as below User.php模型文件应如下

<?php
App::uses('AppModel','Model');
/*
*
* User Model
*
*/
Class User extends AppModel{

/*
*
* actsAs Behaviour
*
*/
    public $actsAs = array('Containable');


/*
*
* hasMany association
*
*/  
    public $hasMany = array(
        'Item' => array(
            'className' => 'Item',
            'foreignKey' => 'item_id',
            )
        );  
}
?>

You can try with below code in the controller. 您可以在控制器中尝试以下代码。

<?php
$contain = array('Item' => array('User'));
$this->set('projects', $this->Project->find('all', array('contain' => $contain)));
?>

Your approach generates too many queries. 您的方法会生成太多查询。 You may want to reduce their quantity by binding and unbinding models. 您可能希望通过绑定和取消绑定模型来减少数量。 See details here . 在这里查看详细信息。

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

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