CakePHP HABTM问题 [英] CakePHP HABTM question
问题描述
在尝试查看所有这些 PHP框架空闲时间时,可能是一个新手问题。
对于初学者,我想添加多个标签到多张照片。我有一个标签模型和mot模型(照片)。
mot模型的片段:
var $ hasAndBelongsToMany = array(
'Tag'=>
array(
'className'=>'Tag',
'joinTable'=>'mots_tags',
'foreignKey'=>'mot_id',
'associationForeignKey'=>'tag_id',
'unique'=> gt;假
)
);
在add()的标签控制器中,我有:
$ this-> Tag-> save($ this-> data);
当print_r'ing $ this->数据时,我看到:
数组
(
[Mot] =>数组
(
[id] => 2
)
[Tag] =>数组
(
[title] => 21e21e
)
)
标签被插入标签表中,但没有任何内容插入mottags(在mot和tag之间加下划线,但在我写时会用斜体表示)而不是成为下划线)表。我的mots_tags数据库模式:(sqlite)
创建表mots_tags(id INTEGER PRIMARY KEY,mot_id INTEGER,tag_id INTEGER)
c
任何提示为什么Cake只写标签表而不是关联表?我没有任何SQL错误。有没有办法查看它是否尝试写入关联表?
解决方案尝试
$ this-> Tag-> saveAll($ this-> data);
编辑:
那么,您肯定需要 saveAll()
。此外,所连接的HABTM模型的阵列需要采用某种特定的格式。如果我没记错的话,它应该看起来像这样:
array(
'Tag'=> array(' title'=> ...),//主模型
'Mot'=> array(//连接的HABTM模型
'Mot'=> array($ id,$ id,$ id,...)
)
);
Might be a newbie question as I'm trying to see what all these "PHP frameworks" are at my free time.
For starters I want to add multiple tags to multiple photos. I have a tags model and mot model (the photos).
Snip of mot model:
var $hasAndBelongsToMany = array(
'Tag' =>
array(
'className' => 'Tag',
'joinTable' => 'mots_tags',
'foreignKey' => 'mot_id',
'associationForeignKey' => 'tag_id',
'unique' => false
)
);
In my tags controller in add() I have:
$this->Tag->save($this->data);
When print_r'ing $this->data I see:
Array
(
[Mot] => Array
(
[id] => 2
)
[Tag] => Array
(
[title] => 21e21e
)
)
Tag get inserted into Tags table, but nothing gets inserted into mottags(theres underscore between mot and tag but it italics when i write it here instead of becoming an underscore) table. My mots_tags db schema: (sqlite)
create table mots_tags (id INTEGER PRIMARY KEY, mot_id INTEGER, tag_id INTEGER)
Any clues why Cake writes only to Tags table and not to associacions table? I don't get any SQL errors. Is there a way to see if it tries to write to associations table at all?
解决方案 Try
$this->Tag->saveAll($this->data);
Edit:
Well, you definitely need saveAll()
. Additionally, the array of the connected HABTM model needs to be in a certain, slightly curious format. If I remember correctly, it should look like this:
array(
'Tag' => array('title' => ...), // primary model
'Mot' => array( // connected HABTM model
'Mot' => array($id, $id, $id, ...)
)
);
这篇关于CakePHP HABTM问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!