CakePHP:从HABTM连接表中检索关联

CakePHP:从HABTM连接表中检索关联,cakephp,Cakephp,在CakePHP2.0中,我们有一个订户模型和一个SubscriberGroup模型。它们都相互关联 在SubscriberGroup编辑操作中,我们希望获得该组中的订阅者列表,这在定义HABTM关系时不是问题。但是,关联表subscriber\u groups\u subscribers也有一个author\u id字段,它是另一个表用户的外键 因此,当我们执行$this->SubscriberGroups->read时,我们不仅要获取组和订阅者,还要从users表中获取创建连接的用户的详细信

在CakePHP2.0中,我们有一个订户模型和一个SubscriberGroup模型。它们都相互关联

在SubscriberGroup编辑操作中,我们希望获得该组中的订阅者列表,这在定义HABTM关系时不是问题。但是,关联表subscriber\u groups\u subscribers也有一个author\u id字段,它是另一个表用户的外键

因此,当我们执行$this->SubscriberGroups->read时,我们不仅要获取组和订阅者,还要从users表中获取创建连接的用户的详细信息

HABTM关系定义如下:

public $hasAndBelongsToMany = array(
    'Subscriber' => array(
         'className' => 'Subscriber',
         'joinTable' => 'subscriber_groups_subscribers',
         'foreignKey' => 'subscriber_group_id',
         'associationForeignKey' => 'subscriber_id',
         'fields' => 'id,first_name,last_name,email_address,last_error',
         'unique' => 'keepExisting'
    )
);

有没有一种方法可以在不定义自定义查询的情况下执行此操作?

要实现此目的,您可能需要使用:

不要忘记为SubscriberGroup模型设置可包含的行为

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

我认为没有办法使用CakePHP与3个表建立HABTM关系,即使是带有一个额外字段的HABTM也很复杂

最简单的方法是创建链接模型并在SubscriberGroup模型上添加hasOne关系:

然后,以下是您通过电话阅读得到的信息:

输出:

array(
    'SubscriberGroup' => array(
        'id' => '1'
    ),
    'LinkModel' => array(
        (int) 0 => array(
            'id' => '2',
            'author_id' => '1',
            'subscriber_id' => '3',
            'subscriber_group_id' => '1',
            'Subscriber' => array(
                'id' => '3'
            ),
            'Author' => array(
                'id' => '1'
            ),
            'SubscriberGroup' => array(
                'id' => '1'
            )
        ),
        (int) 1 => array(
            'id' => '3',
            'author_id' => '1',
            'subscriber_id' => '1',
            'subscriber_group_id' => '1',
            'Subscriber' => array(
                'id' => '1'
            ),
            'Author' => array(
                'id' => '1'
            ),
            'SubscriberGroup' => array(
                'id' => '1'
            )
        ),
        (int) 2 => array(
            'id' => '4',
            'author_id' => '2',
            'subscriber_id' => '1',
            'subscriber_group_id' => '1',
            'Subscriber' => array(
                'id' => '1'
            ),
            'Author' => array(
                'id' => '2'
            ),
            'SubscriberGroup' => array(
                'id' => '1'
            )
        )
    )
)

您在订户和作者之间是否有归属关系?如果是这样,请尝试将$this->SubscriberGroup->recursive设置为3.Holt-我正在寻找关系的作者,而不是订阅者。抱歉,您的问题中跳过了一行。。。我不确定CakePHP与自定义字段之间是否有明确的HABTM关系。我将为HABTM表创建一个模型,并将belongsTo和hasOne添加到该模型与Author、Subscriber和SubscriberGroup模型之间的关系中。
class LinkModel extends AppModel {

    public $useTable  = 'subscriber_groups_subscribers' ;

    public $actAs = array('Containable') ;

    public $belongsTo = array(
        'Subscriber',
        'Author',
        'SubscriberGroup'
    ) ;

}

class SubscriberGroup extends AppModel {
    public $actAs = array('Containable') ;

    public $hasMany = array(
        'LinkModel'
    ) ;
}
$this->SubscriberModel->recursive = 2 ;
$res = $this->SubscriberModel->read(null, 1) ;
debug($res) ;
array(
    'SubscriberGroup' => array(
        'id' => '1'
    ),
    'LinkModel' => array(
        (int) 0 => array(
            'id' => '2',
            'author_id' => '1',
            'subscriber_id' => '3',
            'subscriber_group_id' => '1',
            'Subscriber' => array(
                'id' => '3'
            ),
            'Author' => array(
                'id' => '1'
            ),
            'SubscriberGroup' => array(
                'id' => '1'
            )
        ),
        (int) 1 => array(
            'id' => '3',
            'author_id' => '1',
            'subscriber_id' => '1',
            'subscriber_group_id' => '1',
            'Subscriber' => array(
                'id' => '1'
            ),
            'Author' => array(
                'id' => '1'
            ),
            'SubscriberGroup' => array(
                'id' => '1'
            )
        ),
        (int) 2 => array(
            'id' => '4',
            'author_id' => '2',
            'subscriber_id' => '1',
            'subscriber_group_id' => '1',
            'Subscriber' => array(
                'id' => '1'
            ),
            'Author' => array(
                'id' => '2'
            ),
            'SubscriberGroup' => array(
                'id' => '1'
            )
        )
    )
)