如何在 OneToMany/ManyToOne 上订购 [英] How to OrderBy on OneToMany/ManyToOne

查看:27
本文介绍了如何在 OneToMany/ManyToOne 上订购的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Product类,上面有很多ManyToMany的字段,比如成分、大小、种类等等.一共大概14个不同的字段并非所有字段都与每个产品相关.

I have a Product class that has many fields on it for ManyToMany, such as ingredients, sizes, species, etc.. A total of about 14 different fields Not all of the fields are are relevant to each product.

我有这样的映射设置

Class product {
/**
 * @var Species[]
 * @ORMManyToMany(targetEntity="Species")
 * @ORMJoinTable(name="product_species",
 *      joinColumns={@ORMJoinColumn(name="productId", referencedColumnName="id")},
 *      inverseJoinColumns={@ORMJoinColumn(name="speciesId", referencedColumnName="id")}
 *      )
 * @ORMOrderBy({"name" = "asc"})
 */
private $species;

这对多对多/多对一非常有用.

This works great for a manytomany/manyto one.

问题出在我的 product_ingredients 表中,我需要添加一个额外的字段,这意味着需要从多对多切换到一对多/多对一像这样

The problem is in my product_ingredients table I needed to add an additional field, meaning need to switch from ManyToMany to a OneToMany/ManyToOne So like this

/**
     * @var ProductIngredient[]
     *
     * @ORMOneToMany(targetEntity="ProductIngredient", mappedBy="product")
     * @ORMJoinColumn(name="productId", referencedColumnName="id")
     */
    private $ingredients;

现在我的 ProductIngredient 实体看起来像这样

Now my ProductIngredient Entity Looks like this

 /**
     * @var IngredientType
     * @ORMManyToOne(targetEntity="IngredientType", fetch="EAGER")
     * @ORMJoinColumn(name="ingredientTypeId", referencedColumnName="id")
     */
    private $ingredientType;


    /**
     * @var Ingredient
     *
     * @ORMManyToOne(targetEntity="Ingredient", inversedBy="products", fetch="EAGER")
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="ingredientId", referencedColumnName="id")
     * })
     */
    private $ingredient;

    /**
     * @var Product
     *
     * @ORMManyToOne(targetEntity="Product", inversedBy="ingredients")
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="productId", referencedColumnName="id")
     * })
     */
    private $product;

所以在我的物种产品类中,我使用@ORMOrderBy 以便物种已经被订购.. 有没有办法我也可以以某种方式为我的成分字段执行此操作?

So in my product class for species I use the @ORMOrderBy so that species is already ordered.. Is there a way I can somehow also do this for my ingredients field?

或者我做错了我的逻辑,这些甚至不应该是产品类中的字段,而应该只是通过存储库查找?

Or am I doing my logic wrong and these shouldn't even be fields on the product class and should just be looking up by the repository instead?

我希望它很简单,这样我就可以循环遍历我的对象,比如 $product->getIngredients()而不是做

I was wanting it to be easy so I could loop through my objects like $product->getIngredients() instead of doing

$ingredients = $this->getDoctrine()->getRepository('ProductIngredient')->findByProduct($product->getId());

推荐答案

在 Product 实体中也只是将 orderBy 添加到成分关系中

in the Product entity just also aadd the orderBy to the ingredients relation

/**
 * ...
 * @ORMOrderBy({"some_attribute" = "ASC", "another_attribute" = "DESC"})
 */
private $ingredients;

这篇关于如何在 OneToMany/ManyToOne 上订购的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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