CakePHP HTBTM 带有额外字段:双重逻辑关系:如何自己在控制器中构造 $this->data

发布于 2024-09-16 11:58:42 字数 417 浏览 8 评论 0原文

我在“事件”和“类别”模型之间有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戈亓 2024-09-23 11:58:42

一旦我的连接表具有双键以外的任何东西(在您的情况下可能是 event_idcategory_id ,我将其创建为“独立”模型并将关系修改为如下(再次使用您的示例):

Event -- hasMany --> EventCategory <-- hasMany -- Category
Event <-- belongsTo -- EventCategory -- belongsTo --> Category

“魔力”少了一些,但我认为一旦引入附加属性(即字段),魔力就会开始崩溃。这也为您的未来提供了更多的灵活性。如果您已经找到了引入一项新财产的理由,那么您似乎更有可能找到增加更多财产的理由。

As soon as my joining table has anything more than the dual keys (probably event_id and category_id in your case, I create it as a "standalone" model and modify the relationships as follows (again, using your example):

Event -- hasMany --> EventCategory <-- hasMany -- Category
Event <-- belongsTo -- EventCategory -- belongsTo --> Category

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.

标点 2024-09-23 11:58:42

根据我理解您的要求的方式,您需要在 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.

好多鱼好多余 2024-09-23 11:58:42

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 后,您可以运行查询来查找所有子事件:

$this->Event->find('all', array('conditions' => array('primary_event_id' => $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. The is_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 the events table and make it a bool.

2- Add a primary_event_id to the events 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->Event->find('all', array('conditions' => array('primary_event_id' => $id)));

This will return a list of all sub-events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文