简体   繁体   English

Symfony:如何对实体的One2Many / Many2One进行排序?

[英]Symfony: how to sort One2Many/Many2One of an entity?

I've three entities: Product , ProductPicture and Picture . 我有三个实体: ProductProductPicturePicture

I'd like to sort the pictures by it's position and tried this , code below. 我想按图片的位置对图片进行排序,并在下面尝试了代码。

Result : it's not sorting the joined pictures by position. 结果 :未按位置对加入的图片进行排序。 It's sorting the products by the pictures' position. 它按图片的位置对产品进行排序。

I'm confused. 我糊涂了。 As far as I can see, I followed the docs, but get a weird result. 据我所知,我遵循了文档,但得到了一个奇怪的结果。 Cache was cleared. 缓存已清除。

Any ideas? 有任何想法吗? Thanks in advance! 提前致谢!

Entity: Product 实体: 产品

// ...
class Product {
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    protected $id;


    /**
     * @ORM\OneToMany(targetEntity="ProductPicture", mappedBy="product")
     * @ORM\OrderBy({"position" = "DESC"})    // <------ !!!
     */
    protected $pictures;

    // ...
}

Entity: ProductPicture 实体: ProductPicture

// ...
class ProductPicture {
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    protected $id;


    /**
     * @ORM\Column(type="integer")
     */
    protected $position;


    /**
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="pictures")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
     */
    protected $product;


    /**
     * @ORM\ManyToOne(targetEntity="Picture")
     * @ORM\JoinColumn(name="picture_id", referencedColumnName="id")
     */
    protected $picture;

    // ...
}

Entity: Picture 实体: 图片

// ...
class Picture {
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    protected $id;


    /**
     * @ORM\Column(name="path", type="string")
     */
    protected $path;

    // ...
}

The docs are right: 文档是正确的:

<?php
/** @Entity **/
class User
{
    // ...

    /**
     * @ManyToMany(targetEntity="Group")
     * @OrderBy({"name" = "ASC"})
     **/
    private $groups;
}

In my case the findAll() -method of the repository was overwritten which led to this mess. 在我的情况下, repositoryfindAll()方法被覆盖,这导致了混乱。 Make sure, your customized queries are not in conflict with the doctrine defaults, if you've the same issue. 如果您遇到相同的问题,请确保您的自定义查询与该学说的默认设置没有冲突。 :-) :-)

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

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