简体   繁体   中英

cakephp save a HABTM relation's data without using a View

I am using CakePHP v.2.0 with MySQL.

I have two models, Candidate and Lottery connected with a HABTM relationship.

I want to make a save of this HABTM relation's data in my mysql db, with two restrictions:

1) I am not using a View, that is, i want to construct manually the data array passed as the first parameter in save function - but what's its format?

2) There is no candidates table, as my Candidate model loads data from LDAP (I've followed the tutorial at http://bakery.cakephp.org/articles/psychic/2009/03/12/ldap-models-in-cakephp ). I just want to save in table lotteries and in the join table, candidates_lotteries.

Any ideas would be much appreciated.

As long as your relationships are setup correctly in the models, and you do have the HABTM table, you can simply do the following:

$this->data['Candidate']['id'] = {CANDIDATE_ID};
$this->data['Lottery']['id'] = {LOTTERY_ID};
$this->Lottery->save($this->data);

This should show a new record in the HABTM table.

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.

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