简体   繁体   English

cakephp无需使用View即可保存HABTM关系的数据

[英]cakephp save a HABTM relation's data without using a View

I am using CakePHP v.2.0 with MySQL. 我在MySQL上使用CakePHP v.2.0。

I have two models, Candidate and Lottery connected with a HABTM relationship. 我有两个与HABTM关系相关的模型,候选人和彩票。

I want to make a save of this HABTM relation's data in my mysql db, with two restrictions: 我想在我的mysql数据库中保存此HABTM关系的数据,但有两个限制:

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? 1)我没有使用View,也就是说,我想手动构造作为保存函数中第一个参数传递的数据数组-但是它的格式是什么?

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 ). 2)没有候选者表,因为我的候选模型从LDAP加载数据(我遵循了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: 只要在模型中正确设置了关系,并且您具有HABTM表,就可以简单地执行以下操作:

$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. 这应该在HABTM表中显示一条新记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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