Cakephp - 包含(可包含行为)获取太多 [英] Cakephp - contain (containable behavior) fetches too much

查看:199
本文介绍了Cakephp - 包含(可包含行为)获取太多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我从cakephp文档中了解到的,containable行为的优点之一是能够获取更少的数据,如果你需要更少的数据...

As I understood from the cakephp-documentation, one of the advantages of the 'containable'-Behavior is being able to fetch fewer data if you need fewer data...

但是这在我的用户和用户组之间的连接的情况下似乎不起作用。

But that doesn't seem to work in my case of a connection between users and usergroups.

我的关联看起来像:

    Group
    hasMany: Membership

    User
    hasMany: Membership

    Membership
    belongsTo: User, Group

(我没有使用HABTM,

(I'm not using HABTM, instead use the Model 'Membership' in between to join users and groups).

所有模型都实现了可控制行为。

All Models implement the 'Containable'-Behavior.

现在我想获得一个具有特定ID的组的所有成员,只有它们的id和邮件地址。我的查询是这样建立的:

Now I want to get all the members of a group with a certain id, only their ids and mail-addresses. My query is built like that:

    $members = $this->Membership->find('all', array(
        'conditions'    => array(
                'group_id'      => $id
            ),
        'contain'       => array(
            'User'          => array(
                'fields' => array('id', 'fullName')
            ),
        )
    ));

但结果数组看起来像:

array(
    (int) 0 => array(
            'Membership' => array(
            'id' => '1',
            'group_id' => '1',
            'user_id' => '1',
            'role' => 'member'
        ),
        'Group' => array(
            'id' => '1',
            'name' => 'Foo Group'
        ),
        'User' => array(
            'password' => '*****',
            'id' => '1',
            'name' => 'Dr. Foo'
            'mail' => 'foo@bar.baz',
            'role' => 'admin'
        )
    )
)

所以有更多的字段取得比我想要的...(这是一样的事btw wenn将包含键设置为:

So there are definietely more fields fetched than I wanted to... (it's the same thing btw wenn I set the 'contain'-key to:

    'contain'   => array(
        'User.fullName', 'User.id'
    )

我可以使用containable- / p>

Am I using the containable-behavior wrong?

推荐答案

您的模型似乎并不包含任何内容。

Your models don't seem to be acting containabl-y at all. Have you set your models to act as containable?

class Post extends AppModel {
    public $actsAs = array('Containable');
}

如果是这样,也许问题是递归code> Group 数组与查询)。可控行为应该自己处理递归级别,但尝试在AppModel上设置它,只是为了确保

If so, maybe the problem is with the recursion (to avoid getting the Group array with the query). Containable behavior should handle the recursion level on its own, but try setting it on the AppModel just to be sure

class AppModel extends Model {
    public $actsAs = array('Containable');
    public $recursive = -1;

您的第一次尝试

    'contain'       => array(
        'User'          => array(
            'fields' => array('id', 'fullName')
        ),
    )

在语法方面看起来不错,所以它可能是 actAs

looks good in terms of syntax, so it probably the actAs thing.

此外,对于调试,请尝试

Also, for debugging also, try

$this->Membership->contain('User');
$this->Membership->find('all', array(
    'conditions'    => array(
            'group_id'      => $id
        ));

,看看你是否得到预期的结果。

and see if you get the expected results that way.

这篇关于Cakephp - 包含(可包含行为)获取太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆