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

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

问题描述

我有一个产品类,在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[]
 * @ORM\ManyToMany(targetEntity="Species")
 * @ORM\JoinTable(name="product_species",
 *      joinColumns={@ORM\JoinColumn(name="productId", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="speciesId", referencedColumnName="id")}
 *      )
 * @ORM\OrderBy({"name" = "asc"})
 */
private $species;

这对于manytomany / manyto一个非常有用。

This works great for a manytomany/manyto one.

问题出在我的product_ingredients表中,我需要添加一个附加字段,这意味着需要从ManyToMany切换到OneToMany / ManyToOne
所以像这样

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[]
     *
     * @ORM\OneToMany(targetEntity="ProductIngredient", mappedBy="product")
     * @ORM\JoinColumn(name="productId", referencedColumnName="id")
     */
    private $ingredients;

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

Now my ProductIngredient Entity Looks like this

 /**
     * @var IngredientType
     * @ORM\ManyToOne(targetEntity="IngredientType", fetch="EAGER")
     * @ORM\JoinColumn(name="ingredientTypeId", referencedColumnName="id")
     */
    private $ingredientType;


    /**
     * @var Ingredient
     *
     * @ORM\ManyToOne(targetEntity="Ingredient", inversedBy="products", fetch="EAGER")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="ingredientId", referencedColumnName="id")
     * })
     */
    private $ingredient;

    /**
     * @var Product
     *
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="ingredients")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="productId", referencedColumnName="id")
     * })
     */
    private $product;

所以在我的产品类中,我使用@ ORM\OrderBy,以便物种已经订购有没有办法我也可以为我的配料领域做这个?

So in my product class for species I use the @ORM\OrderBy 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());


推荐答案

在产品实体中也只是添加订单成分关系

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

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

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

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