在Doctrine 2中获取相关联的条目 [英] Getting associated entries in Doctrine 2

查看:120
本文介绍了在Doctrine 2中获取相关联的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经决定,熟悉ORM是一个很好的资产,并且将Doctrine 2作为选择的ORM。

I've decided that it would be a good asset to get familiar with an ORM and went for Doctrine 2 as the ORM of choice.

我正在工作在一个测试项目上学习教义的基础知识。虽然大多数人通常都会使用博客,但我决定制作一个基本的应用程序,您可以在其中保存和跟踪订单。我的数据库模式如下:

I'm working on a test project to learn the basics of Doctrine. Although most people usually go with a blog, I've decided to make a basic app in which you can save and track orders. My database schema would be as follows:

User
    id
    name
Product
    id
    name
    price
Sales_order
    id
    user_id
    product_id
    quantity
    unit_price

因此,我的订单模型如下所示:

Hence, my Order model looks like:

/**
 * @Entity
 * @Table(name="sales_order")
 */
class Order {

    /**
     * @Id
     * @Column(type="integer", nullable=false)
     * @GeneratedValue(strategy="AUTO")
     */
    private $Id;

    /**
     * @OneToOne(targetEntity="User", inversedBy="user")
     */
    private $user;

    /**
     * @OneToOne(targetEntity="Product", inversedBy="product")
     */
    private $product;

    /**
     * @Column(type="integer", nullable=false)
     */
    private $quantity;
}

现在,问题是,是否有一种简单的访问所有订单的方法从用户模型?我应该为这些基本的东西写DQL(教义查询语言)还是有方法轻松获取关联实体?我的意思是说,没有任何其他的观点,对吧?此外,我正在做这些协会吗?我真的很困惑在这个非常基本的模型...详细的帮助是非常感谢。谢谢。

Now, the question is, is there a simple way of accessing all the orders from the user model? Should I write DQL (doctrine query language) for these kind of basic stuff or is there a way to easily get associated entities? I mean, there wouldn't be any point to this otherwise, right? Also, am I doing these associations correctly? I'm really confused in this very basic model... Detailed help is really appreciated. Thank you.

推荐答案

首先,不用担心数据库设计。您应该设计您的实体,并使用 SchemaTool

Firstly, don't worry too much about the database design. You should design your entities and use the SchemaTool.


现在,问题是,是否有一种简单的访问方式来自用户模型的订单?

Now, the question is, is there a simple way of accessing all the orders from the user model?

您是否意味着访问用户模型中的所有订单,或访问与一个用户相关联的所有订单?

Do you mean access all of the orders from the user model, or access all the orders associated to a user?

如果您的意思是前者,那么您做错了(见下文)。如果你的意思是以后,你应该设置一个双向关系。 (BTW,OneToMany不是OneToOne,因为一个用户可能会有很多订单)。

If you meant the former, well you are doing things wrong (see below). If you meant the later, you should setup a bi-directional relationship between orders and users. (BTW, it would be OneToMany not OneToOne as one user would likely have many orders).


<我真的很困惑这个非常基本的模型...

I'm really confused in this very basic model...

我认为你有麻烦 - 与许多PHP程序员 - 是 DataMapper模式以及最终的域驱动设计的基本理解。记住,你正在处理持久性对象,而不是数据库表。

I think what you having trouble with - along with many PHP programmers - is the fundamental understandings of the DataMapper pattern and ultimately Domain Driven Design as well. Remember, you are dealing with persistable objects, not database tables.

我不能在这里提供详细的信息,因为我会写一本书,因此我建议你一本关于域驱动设计的书,以帮助您从原理开始。有一些很好的在线资源可用,例如 Federico Cargnelutti ,但是它们不是针对教义2的。

I cannot provide detailed information here because I'd be writing a book, hence this I would recommend you get a book on Domain Driven Design to help kick start with the principles. There are a few good online resources available, like a series of blog posts by Federico Cargnelutti, however they aren't specific to Doctrine 2.

这篇关于在Doctrine 2中获取相关联的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆