Yii:引用多个表中的行的最佳方式?

发布于 2024-10-16 04:37:51 字数 258 浏览 1 评论 0原文

引用任何模型记录的最 Yii 友好的“标准”方式是什么?

我正在尝试构建一个菜单系统,用户可以在其中创建链接到网站上另一个项目的菜单项。显然,用户将能够链接到各种类型的内容(模型)。我会在不使用 url 作为内部引用的情况下完成此操作,以给我更大的灵活性。

因此,正如下面的评论中提到的某种“统一主键”,

我认为每个模型都是基本模型的扩展并引用它,就像

也许另一种方法是将模型和记录 ID 存储在一个字段中,然后覆盖活动记录关系“魔法”

What is the most Yii friendly 'standard' way to reference a record of any model?

I'm attempting to build a menu system where a user can create a menu item which links to another item on the site. Obviously a user would be able to link to a variety of types of content (models). I would to work this without using urls as the internal reference to give me greater flexibility.

So as mentioned in the comment bellow some sort of 'unified primary key'

I was thinking either every model is an extension of a base model and reference that, like in this question. Or to simply have a separate field for each model that can be referenced.

Maybe another way to do it would be to store the model and record id in a field and then just override active records relational 'magic'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

战皆罪 2024-10-23 04:37:51

你想要做的是一个棘手的问题,因为 mySql 只支持第一范式关系而不支持 OOP 继承。这称为“多态关联”。不过有几个解决方案。可以在这里找到描述其中一些的非常好的答案:

可以对两个可能的表之一执行 MySQL 外键吗?

一个可能的解决方案示例(我认为是上面链接中列出的第二个)与 Yii可能是拥有一个带有“类型”字段的“对象”表,然后使用其他类型“对象”所需的字段分隔表。然后在基本对象模型的 afterFind() 方法中,查看类型字段并附加类型 允许访问类型表列的行为

另外,如果您确实只是想要一种更简单的方法来创建链接或短 URL,您可以在框架中编写一些额外的函数,这些函数基本上只是使链接到对象变得更容易。像平常一样为所有对象类型拥有单独的模型,但有一个带有主主键(或 URL slug)的中央表,该主键与各个类型建立关系。您可以重写 createUrl() ,以便它只接受主密钥(无论内容类型如何),然后通过查找主表中要使用的模型/控制器来构建正确的链接。

这就是 Drupal 的 URL 别名允许您做的事情。比重新创建类似 Drupal CCK 的系统更容易!无论如何,这都是不必要的,除非您确实需要将来灵活地更改内容。

What you are trying to do is a kind of a tough problem, because mySql only supports First Normal Form relationships and not OOP inheritance. This is called "polymorphic association". There are a couple solutions though. A really good SO answer which describes some of them can be found here:

Possible to do a MySQL foreign key to one of two possible tables?

One possible solution example (the second one listed in the link above, I think) with Yii might be to have a single "object" table with a "type" field, and then then separate tables with the fields needed for the other types of "objects". Then in the afterFind() method of the base Object model, look at the type field and attach type Behaviors to them that allow access to the type table columns.

Also, if you really just want an easier way to make links or short URLs, you could write some extra functions in the framework which basically just make linking to the object easier. Have separate models for all of the object types just like normal, but have a central table with a master Primary Key (or URL slug) which makes a relation to the individual types. You could override createUrl() so it just takes in the master key (regardless of content type) and then build the correct link by looking up what model/controller to use in the master table.

This is sort of what Drupal's URL aliases allow you to do. Easier than recreating a Drupal CCK like system! Which would be unnecessary anyway unless you really needed the flexibility to change the content in the future.

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