[英]Symfony2 Sonata admin setting query for sonata_type_collection
Is it possible to set up a query for sonata_type_collection? 是否可以为sonata_type_collection建立查询? For example II simply do this now... 例如II现在就要做
->add('deal', 'sonata_type_collection',
array(
'required' => true,
'label' => 'product',
),
array(
'property' => 'product',
'edit' => 'inline',
'inline' => 'table',
))
This works fine, but the deals that I am showing in the collection have statuses... And I only want to display the deals that have statuses set up to true... How can I do that? 效果很好,但是我在收藏夹中显示的交易具有状态...而且我只想显示状态设置为true的交易...我该怎么做? Can I set up a query for collection? 我可以设置要收集的查询吗?
The thing is I can do this: 事情是我可以做到这一点:
->add('deal', null, array(
'by_reference' => false,
'class' => 'Mp\ShopBundle\Entity\DailyDeal',
'query_builder' => $this->modelManager->createQuery('Mp\ShopBundle\Entity\DailyDeal', 'h')
->where('h.status = 1'),
), array(
'edit' => 'inline',
'inline' => 'table',
))
And this way if is select multiple deals it saves in an array... I want to create a seperate object for every deal I select. 这样,如果选择了多个交易,它将保存在数组中...我想为我选择的每个交易创建一个单独的对象。 Like the sonata_type_collection does.. 就像sonata_type_collection一样。
What could I do? 我能做什么?
The only way that I know is to create a property and getter in your Entity that only return your desired values and use it in your Admin class. 我知道的唯一方法是在Entity中创建属性和getter,该属性和getter仅返回所需的值并在Admin类中使用它。
Entity.php Entity.php
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
class Entity
{
/**
* @ORM\OneToMany(targetEntity="XXX")
*/
private $dealsActivated;
public function getDealsActivated()
{
$collection = new ArrayCollection();
foreach ($this->getDeal() as $deal) {
if ($deal->getStatus() === 1) {
$collection->add($deal);
}
}
return $collection;
}
}
Admin.php Admin.php
->add('dealsActivated', 'sonata_type_collection',
array(
'required' => true,
'label' => 'product',
),
array(
'property' => 'product',
'edit' => 'inline',
'inline' => 'table',
)
)
It's not optimized but works with Sonata. 它尚未优化,但可与Sonata一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.