CakePhp:建模递归关联并查找

发布于 2024-09-03 04:29:14 字数 442 浏览 7 评论 0原文

我在 CakePhp 上的模型上使用 find() 时遇到了一些问题。

我有三个以这种方式相关的模型:

Project(some_fields, item_id) ------属于----->项目(某些字段,项目 ID) ------属于-----> User(some_fieldscampi,nickname)

我需要执行 find() 并检索项目中的所有字段、项目中的字段和用户中的昵称字段。这是我的代码:

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

但我的输出不包含用户对象。 我尝试过 Containable 行为,但输出是相同的。什么坏了?

非常非常感谢

彼得

I've some trouble with a find() on a model on CakePhp.

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)

I need to do a find() and retrieve all fields from project, a field from Item and the nickname field from User. 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. What is broken?

Many many Thanks

Peter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩人痒 2024-09-10 04:29:14

您的方法会产生太多查询。您可能希望通过绑定和取消绑定模型来减少它们的数量。请参阅此处了解详细信息。

Your approach generates too many queries. You may want to reduce their quantity by binding and unbinding models. See details here.

獨角戲 2024-09-10 04:29:14

正确设置您的模型或尝试加入

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

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
   );
许久 2024-09-10 04:29:14

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 模型文件应如下所示

<?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 模型文件应如下所示

<?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',
            )
        );  
}
?>

您可以在控制器中尝试使用以下代码。

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

Project.php Model file should be as below

<?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

<?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

<?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)));
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文