I'm trying for 3 days to make it work, already apply all i have seen on stack/forums/google. A subscribers habtm subrcriberlist and a subscriberslist can belongs to many subscribers.
/* Subscriber model*/
public $hasAndBelongsToMany =
array(
'Subscriberslists' => array(
'className' => 'Subscriberslist',
'joinTable' => 'subscribers_subscriberslists',
'foreignKey' => 'subscriber_id',
'associationForeignKey' => 'subscriberslist_id',
'unique' => true
)
);
/* Subscriberslist model*/
public $hasAndBelongsToMany =
array(
'Subscribers' => array(
'className' => 'Subscriber',
'joinTable' => 'subscribers_subscriberslists',
'foreignKey' => 'subscriberslist_id',
'associationForeignKey' => 'subscriber_id',
'unique' => true
)
);
Datas are formatted like indicated in all post i have seen :
array(
'Subscriberslist' => '1',
'Subscriber' => array(
'Subscriber' => array(
(int) 0 => '1',
(int) 1 => '2',
(int) 2 => '3'
)
)
Then i could not save my data in my join table. Cake only save new Subscribers. Here is my controller :
if (!$this->Subscriber->saveAll($this->request->data){ //error}else{//no error}
Could you tell me what I'm doing wrong ? Thanks
I would say that if you're trying to insert a subscriber, the saveAll
method is expecting something like the following:
array(
'Subscriber' => array(/* Single subscriber fields here */),
'Subscriberslist' => array(
/* List subscribers here */
)
)
Try using the saveAssociated
method first because that is expecting a single instance of the model you're trying to save but will persist the associated data with it. If you need to save multiple subscribers at once then change up a gear and revert to saveAll
:
array(
array(
'Subscriber' => array(/* Single subscriber fields here */),
'Subscriberslist' => array(
/* List subscribers here */
)
),
array(
'Subscriber' => array(/* Single subscriber fields here */),
'Subscriberslist' => array(
/* List subscribers here */
)
)
)
I hope this was of some help!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.