CakePHP HTBTM 带有额外字段:双重逻辑关系:如何自己在控制器中构造 $this->data
我在“事件”和“类别”模型之间有 HABTM 关系。但在桥接表“categories_events”表中,有一个额外的字段,名为“is_primary”;含义和事件属于 1 个主要类别和许多次要类别。
我部署了 1 个名为“primary_event”的选择框和名为“seconday_event[]”的复选框。
当我提交表单时,数据会像这样出现。 $this->data['Event']['primary_event'] ->; 4; $this->data['Event']['secondary_event'] ->;数组(1,3,5);
之后,我如何将这些数据输入 Cake 识别的格式以及调用哪个 Model 的保存函数?请注意,“primary_event”行的“is_primary”字段必须设置为 1。
谢谢, 萌
I have HABTM relation between "Event" and "Category" models. But in the bridge table, "categories_events" table, there is an extra field called "is_primary"; meaning and Event is belong to 1 primary category and many secondary categories.
I deployed 1 select box named "primary_event" and checkboxes named "seconday_event[]".
When I submit the form the data comes up like this.
$this->data['Event']['primary_event'] -> 4;
$this->data['Event']['secondary_event'] -> array(1,3,5);
After that, how do I feed those data into the format that Cake recognize and which Model's save function do I call? Please be advised that "is_primary" field must be set to 1 for 'primary_event' row.
Thanks,
Moe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一旦我的连接表具有双键以外的任何东西(在您的情况下可能是
event_id
和category_id
,我将其创建为“独立”模型并将关系修改为如下(再次使用您的示例):“魔力”少了一些,但我认为一旦引入附加属性(即字段),魔力就会开始崩溃。这也为您的未来提供了更多的灵活性。如果您已经找到了引入一项新财产的理由,那么您似乎更有可能找到增加更多财产的理由。
As soon as my joining table has anything more than the dual keys (probably
event_id
andcategory_id
in your case, I create it as a "standalone" model and modify the relationships as follows (again, using your example):There's a little less "magic", but I think the magic begins to break down as soon as you introduce an additional property (i.e. field). This also gives you a little more flexibility for the future. If you've already found a reason to introduce one new property, it seems even more likely that you might find a reason to add more.
根据我理解您的要求的方式,您需要在
categories_events
模型上创建和保存记录(除非模型有特殊行为,否则您不需要显式编码模型 - CakePHP 足够明智地理解你正在做什么并使用默认的模型结构)。第一个创建和保存将是主要的,然后循环遍历辅助数组。请记住在每次迭代时执行
my_model->create()
。The way I understand your requirement, you'll need to create and save records on the
categories_events
model (You don't need to explicitly code the model unless it has special behaviour - CakePHP is wise enough to understand what you're doing and use a default model structure).The first create and save will be the primary, then loop over the array for the secondaries. Remember to do a
my_model->create()
on each iteration.is_primary
是事件的函数,而不是您尝试将其关联到的 HABTM 表。 HABTM 链接事件和类别,而不是事件与事件。is_primary
应存储在事件表中。然后,您需要将子事件与其主事件相关联。以下是如何做到这一点。1- 将
is_primary
字段添加到events
表中,并将其设置为布尔值。2- 将
primary_event_id
添加到events
表中。 (如果事件是子事件,则仅包含一个值。)3- 在事件模型中创建
事件
与其自身之间的关系。 (http://book.cakephp.org/view /851/Multiple-relations-to-the-same-model)这将允许自引用,以便您可以将子事件引用到其主要事件。获得感兴趣的事件 ID 后,您可以运行查询来查找所有子事件:
这将返回所有子事件的列表。
is_primary
is a function of Events, not the HABTM table you are trying to associate it to. The HABTM links the Events and Categories, not events to events. Theis_primary
should be stored on the Events table. Then you need to associate sub events to their primary event. Here is how to do that.1- Add the
is_primary
field to theevents
table and make it a bool.2- Add a
primary_event_id
to theevents
table. (This will only contain a value if the event is a sub-event.)3- Create a relationship between
events
and itself in the Event model. (http://book.cakephp.org/view/851/Multiple-relations-to-the-same-model) This will allow self referencing so you can reference sub-events to their primary event.Once you get the ID of the event you are interested in, you can run queries to find all of the sub-events:
This will return a list of all sub-events.