教义2中如何确定表关系是双向还是单向?

发布于 2024-11-19 21:05:52 字数 305 浏览 0 评论 0原文

我正在我的 Zend 应用程序中从 Doctrine 1.1.4 升级到 Doctrine 2.0.6。

目前,我正在研究实体之间的关联映射。在 Doctrine 2 的文档中它说“关系可能是双向的,也可能是单向的”。我对这些术语在给定上下文中的含义感到困惑。

如何确定关系是单向还是双向?

感谢您的帮助。

I am in the process of upgrading from Doctrine 1.1.4 to Doctrine 2.0.6 in my Zend application.

Currently, I am working on mapping the associations between entities. In Doctrine 2's Documentation it says 'relationships maybe bidirectional or unidirectional. I am confused as to what these terms mean within the given context.

How do I determine if a relationship is unidirectional or bidirectional?

Appreciate the help.

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

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

发布评论

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

评论(2

时光匆匆的小流年 2024-11-26 21:05:52

如果两个实体都包含对另一个实体的引用,则关系是双向的。

如果您省略其中一个引用,则它是单向的。

考虑一个典型的“帖子”和“标签”模式。通常,您会实现双向关联:

<?php

class Post {
    // ...

    /** 
     * @ManyToMany(targetEntity="Tag",inversedBy="posts")
     */
     protected $tags;

    // ...
}

class Tag {
    // ...

    /**
     * @ManyToMany(targetEntity="Post",mappedBy="tags")
     */
     protected $posts

    // ...
}

现在,假设您决定从不(或很少)需要回答诸如“哪些帖子有标签‘foo’?”之类的问题。您可以省略 Tag 实体中的 $posts 关联,将其转换为单向关联,并减轻 ORM 的一些负载。

您仍然可以回答此类问题,但您必须编写代码才能做到这一点。

事实上,这可能是帖子/标签场景中的一个好方法,因为您通常不会从标签中添加/删除帖子。通常,您只能在帖子中添加/删除标签。当查找“带有标签‘x’的所有帖子”时,您只会从“标签”转到“帖子”,这可以在某种服务类中轻松实现。

A relationship is bidirectional if both entities contain a reference to the other.

If you omit one of those references, it's unidirectional.

Consider a typical "posts" and "tags" schema. Typically, you'd implement a bidirectional association:

<?php

class Post {
    // ...

    /** 
     * @ManyToMany(targetEntity="Tag",inversedBy="posts")
     */
     protected $tags;

    // ...
}

class Tag {
    // ...

    /**
     * @ManyToMany(targetEntity="Post",mappedBy="tags")
     */
     protected $posts

    // ...
}

Now, imagine you decided you never (or rarely) needed to answer questions like "Which posts have Tag 'foo'?". You could omit the $posts association in your Tag entity, converting it to a unidirectional association, and take some load off of the ORM.

You could still answer that kind of question, but you'd have to write code to do it.

In fact, it's probably a good way to go in Posts/Tags scenario, as you wouldn't typically be adding/removing Posts from Tags. Typically, you'd add/remove tags from posts only. You'd only ever go from Tags to Posts when looking for "all posts with tag 'x'", which could be trivially implemented in a service class of some sort.

尴尬癌患者 2024-11-26 21:05:52

与 timdev 的答案相同,

单向 & BiDirectional只是ORM概念,这些与数据库无关,
假设您有一个 OneToMany 关系 -

用户有博客

因此您可以将其作为 OneToMany 属性添加到您的用户实体中

,但显然存在 ManyToOne 关系

< strong>博客有用户,

因此您可以选择在博客实体中创建多对一关系,如果您想从博客实体访问用户,请添加此属性,如果您不想,则无需添加,这不是必需的。 在这两种情况下(无论是否添加双向引用)ORM 将维护相同的数据库结构(博客表将具有 user_id 列)。

Same as timdev`s answer,

Unidirectional & BiDirectional is just ORM Concepts, these have nothing to do with database,
Suppose you have a OneToMany relation -

user has blogs

So you can add this to your User Entity as OneToMany Property

but obviously there exisits ManyToOne Relation

Blogs Has User

so it is optional for you to create a ManyToOne relation in your Blog Entity, If you want to access user from blog entity then add this property if you dont want then dont add,its not necessary. in both Cases (you are adding bidirectional reference or not) ORM will maintain same database structure (blog table will have user_id column) .

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