简体   繁体   English

Symfony2-实体和关系注释

[英]Symfony2 - Entity and Relationship Annotation

This is classic method for the entity relationship; 这是实体关系的经典方法。

/**
 * @ORM\OneToMany(targetEntity="Product", mappedBy="category")
 */
protected $products;

and other entity; 和其他实体;

/**
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
 */
protected $category;

But, when I use this method, I can't use limit function. 但是,当我使用此方法时,不能使用限制功能。 All results are taken. 所有结果均已记录。 But, I should use sql limit function. 但是,我应该使用sql限制功能。 So, I use a repository class. 因此,我使用一个存储库类。 I wrote all sql code again. 我再次编写了所有sql代码。 I used join function again. 我再次使用了join函数。 Is the relationsip annotation necessary in this situation? 在这种情况下,是否需要Relationip批注? Do I still need to add this annotation? 我还需要添加此注释吗?

Unfortunately, there is no way of doing what are you asking. 不幸的是,您无法做任何事情。 You are gonna have to write a custom DQL, preferably inside a custom repository in order to fetch exactly what you want. 您将必须编写自定义DQL,最好是在自定义存储库中编写一个DQL,以便准确获取所需的内容。 Of course you still need the annotation itself, as is required by Doctrine to do many things under the hood and, in general, to keep a good relationship model between your entities. 当然,您仍然需要注释本身,这是Doctrine要求的,它可以在后台执行许多操作,并且通常在实体之间保持良好的关系模型。

The most close annotation you have to control the way in which relationships are joined when using automatic method is the OrderBy one: 使用自动方法时,用于控制关系关系方式的最接近的注释是OrderBy

/*
 * @OneToMany(...)
 * @OrderBy({"name" = "ASC"})
 */
 protected $relation;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM