本文介绍了在Symfony2中与KnpPaginatorBundle排序ManyToOne关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Symfony2项目中使用KnpPaginatorBundle。我有两个实体有很多关系。 / **
* @ 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;
...
}
现在我要排序与KnpPaginatorBundle
< {{entities.sortable('bar','i.bar')| raw}}
我收到以下错误消息
给定的查询组件中没有这样的字段[bar],由[i]
有没有办法使用bar.name在foo中进行排序?
干杯
解决方案
您必须在您给KnpPaginatorBundle的查询中加入表。
SELECT i
FROM abc\DemoBundle\Entity\Bar i
JOIN i.bar b
现在您可以使用以下内容进行排序:
< {{entities.sortable(' bar','b.name')| raw}}>
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
解决方案
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 }}>
这篇关于在Symfony2中与KnpPaginatorBundle排序ManyToOne关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!