简体   繁体   English

CakePHP-在find()方法中从JOIN中排除模型

[英]CakePHP - Exclude Models from JOIN in find() method

I'm currently working on a project with CakePHP and its find() method to handle database queries. 我目前正在使用CakePHP及其find()方法来处理数据库查询的项目。

My current situation is as follows: 我目前的情况如下:

I have 3 Models: User , Location and Order . 我有3个模型: UserLocationOrder The connection is that a User hasOne Location and User hasMany Orders . 连接是User hasOne Location有一个User hasOne LocationUser hasMany Orders

When I'm using the find() method to get the User and the address (stored in the Location model) CakePHP returns the User, Location, and the Order Models. 当我使用find()方法获取用户和地址(存储在位置模型中)时,CakePHP返回用户,位置和订单模型。 In my case, I don't need the Order information. 就我而言,我不需要订单信息。

So my question is: Is it possible to tell CakePHP not to join with the Order Model? 所以我的问题是:是否可以告诉CakePHP不加入订购模型?

I know about the recursive attribute, but if I set it to -1 , CakePHP returns just the User Model and in the case of recursive >= 0, it returns all 3 Models. 我知道recursive属性,但是如果将其设置为-1 ,则CakePHP仅返回用户模型,并且在递归> = 0的情况下,它将返回所有3个模型。

Solved problem with Containable of cakephp. 解决了cakephp的Containable问题。 The code is as follows: 代码如下:

$this->User->Behaviors->load('Containable');

$this->User->recursive = -1;
$this->paginate = array('fields' => array('User.*'),
                        'contain' => array('Location'=>array('field1', 'field2')
                       );

$user = $this->Paginator->paginate('User');

You can use unbind model function to exclude desired models 您可以使用解除绑定模型功能排除所需模型

In your case : 在您的情况下:

$this->User->unbindModel(array('hasMany' => 'Orders'));

For common 对于普通

$this->User->unbindModel(
            array(
                'hasMany' => array('Model1','Model2'),
                'hasOne' => array('Model1','Model2'),
                'belongsTo' => array('Model1','Model2'),
                )
        );

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

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