Sort over ManyToOne Relationship with KnpPaginator

2019-06-28 08:21发布

I'm using KnpPaginatorBundle in a Symfony2 Project. I got 2 Entities with a manyToOne Relationship.

/**
 * @ORM\Entity
 * @ORM\Table(name="foo")
 */
class foo {
   ...

   /**
     * @ORM\ManyToOne(targetEntity="abc\DemoBundle\Entity\Bar")
     * @ORM\JoinColumn(name="bar_id", referencedColumnName="id")
     */
    protected $bar;

    ...
}

/**
 * @ORM\Entity
 * @ORM\Table(name="bar")
 */
class Bar {
   ...

    /**
     * @ORM\Column(type="string", length=50, nullable=true)
     */
    protected $name;

    ...
}

Now I want to sort with the KnpPaginatorBundle

<{{  entities.sortable('bar', 'i.bar')|raw }}

I get following error message

There is no such field [bar] in the given Query component, aliased by [i]

Is there any way to make bar in foo sortable using bar.name?

Cheers

2条回答
倾城 Initia
2楼-- · 2019-06-28 08:52

I believe that sortable takes a label and a column. So you're probably looking for

<{{ entities.sortable('Name', 'i.name')|raw }}
查看更多
3楼-- · 2019-06-28 09:01

You have to JOIN the table in the query you are giving to the KnpPaginatorBundle

SELECT i
FROM abc\DemoBundle\Entity\Bar i
JOIN i.bar b 

Now you can sort with the following:

<{{  entities.sortable('bar', 'b.name')|raw }}>
查看更多
登录 后发表回答