In CakePHP 2.0, we have a Subscriber model and a SubscriberGroup model. They are both linked HABTM to each other.
In our SubscriberGroup edit action, we want to get a list of subscribers in this group, which is not a problem when the HABTM relationship is defined. However, the association table, subscriber_groups_subscribers, also has an author_id field, which is a foreign key to another table - users.
So when we do $this->SubscriberGroups->read(), we want to get not only the group and the subscribers, but also the details from the users table regarding the user who created the connection.
The HABTM relationship is defined as follows:
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'
)
);
Is there a way to do this without defining a custom query?
belongsTo
(orhasOne
) relationship betweenSubscriber
andAuthor
? If so, try to set$this->SubscriberGroup->recursive
to3
. – HoltbelongsTo
andhasOne
relationship between this model andAuthor
,Subscriber
andSubscriberGroup
models. – Holt