CakePHP beforeSave & 保存前HABTM
我正在尝试在 CakePHP 2.0 beta 中编写一些 beforeSave 逻辑。基本上我希望用户能够提交一些文本以实现类似标签的功能。 beforeSave 函数将搜索相关表,如果标签存在,则将其链接到主记录,如果不存在,则创建新标签并保存。然后链接它。
这是我的函数(在 beforeSave 中):
function beforeSave() {
if(!empty($this->data['Term']) && isset($this->Term)){
$terms = $this->Term->find('list');
$terms = array_flip($terms);
foreach($this->data['Term'] as $key => $term){
if(!empty($terms[$term['name']])){
$this->data['Term']['Term'][$key]['id'] = $terms[$term['name']];
unset($this->data['Term'][$key]);
}else{
$this->Term->create();
$this->Term->save(array('Term' => array('name' => $term['name'])));
$this->data['Term']['Term'][$key]['id'] = $this->Term->id;
unset($this->data['Term'][$key]);
}
}
}
return true;
}
这基本上按照我想要的方式工作,在必要时创建记录并查找现有记录,创建一个像这样的数组:
Array
(
[Project] => Array
(
[id] => 2
[title] => Project Title
...
)
[Term] => Array
(
[Term] => Array
(
[0] => Array
(
[id] => 10
)
[1] => Array
(
[id] => 2
)
)
)
)
它还成功地将数据保存在主模型(项目)中。但 [Term][Term] 下的关联将被忽略。我非常确定这是保存 HABTM 关联的正确数组结构。
有人看出这有什么问题吗?
I'm trying to write some beforeSave logic in CakePHP 2.0 beta. Basically I want the user to be able to submit some text for tag-like functionality. The beforeSave function would search a related table, if the tag exists just link it to the primary record, and if not, create the new tag & then link it.
So here's my function (in beforeSave):
function beforeSave() {
if(!empty($this->data['Term']) && isset($this->Term)){
$terms = $this->Term->find('list');
$terms = array_flip($terms);
foreach($this->data['Term'] as $key => $term){
if(!empty($terms[$term['name']])){
$this->data['Term']['Term'][$key]['id'] = $terms[$term['name']];
unset($this->data['Term'][$key]);
}else{
$this->Term->create();
$this->Term->save(array('Term' => array('name' => $term['name'])));
$this->data['Term']['Term'][$key]['id'] = $this->Term->id;
unset($this->data['Term'][$key]);
}
}
}
return true;
}
This basically works the way I want, creating records where necessary and finding the existing records, creating an array like this:
Array
(
[Project] => Array
(
[id] => 2
[title] => Project Title
...
)
[Term] => Array
(
[Term] => Array
(
[0] => Array
(
[id] => 10
)
[1] => Array
(
[id] => 2
)
)
)
)
It also successfully saves the data in the primary model (Project). But the associations under [Term][Term] are ignored. I'm pretty sure this is the correct array structure to save HABTM associations.
Anyone see what is wrong with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在视图中,您应该有 1 个输入文本
$form->input('terms');
以便用户可以输入所有标签。因此,在 beforeSave 中,您将拥有一串标签(或术语),您可能希望将其切片为数组,find('list') 术语以及一些数组相交或其他内容。但最终,您需要数据看起来像这样才能使用 saveAll:in the view you should have 1 input text
$form->input('terms');
so the user can enter all the tags. So in beforeSave, you would have a string of the tags (or terms), you might want to slice it out to array, find('list') of the terms, and some array intersect or something. But in the end, you need the data to look something like this to use saveAll: