我可以使用表单构建器中的查询以symfony形式获取过滤收集 [英] Can i use query in form builder to get filtered collection in symfony form

查看:117
本文介绍了我可以使用表单构建器中的查询以symfony形式获取过滤收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AcmePizza BUndle这是正常工作

   - > add('pizza','entity',array 
'class'=>'Acme\PizzaBundle\Entity\Pizza',
'query_builder'=> function($ repository){return $ repository-> createQueryBuilder('p' ) - > orderBy('p.name','ASC');},
))

我可以在收藏中做这样的事情

   - > add('userTasks','collection',array ('type'=> new UserTaskType(),
'class'=>'acme\myBundle\Entity\UserTask',
'query_builder'=> function($ repository) {return $ repository-> createQueryBuilder('p') - > orderBy('p.name','ASC');},
))
pre>

解决方案

假设你的userTasks是一个关系你将为您的案例此处找到答案。这些只是如何排序,但如果你需要一些WHERE条件,它不是那么简单,但也不是很难。



我不得不过滤掉一些实体,关键解决它是在实体类中创建set / get方法返回所需的集合。



在我的情况下,它看起来像这样

  / ** 
*设置值
*
* @param ArrayCollection $ values
* @return属性
* /
public function setCustomValues($ values)
{
$ result = $ this-> getNotCustomValues();
foreach($ values as $ value)
{
$ value-> setAttribute($ this);
$ result-> add($ value);
}
$ this-> values = $ result;

return $ this;
}

/ **
*获得值
*
* @return \Doctrine\Common\Collections\Collection
* /
public function getCustomValues()
{
$ result = new ArrayCollection();
foreach($ this-> value as $ value){
if($ value-> getCustom()){
$ result-> add($ value);
}
}
return $ result;
}

当创建表单时,字段的名称为customvalues而不是值
所以我的集合只包含带有自定义字段true的值。


IN the AcmePizza BUndle this is working fine

->add('pizza', 'entity', array(
                'class'         => 'Acme\PizzaBundle\Entity\Pizza',
                'query_builder' => function ($repository) { return $repository->createQueryBuilder('p')->orderBy('p.name', 'ASC'); },
            ))

Can i do something like that in collection

->add('userTasks','collection',array('type' => new UserTaskType(),
                    'class'         => 'acme\myBundle\Entity\UserTask',
                    'query_builder' => function ($repository) { return $repository->createQueryBuilder('p')->orderBy('p.name', 'ASC'); },
                ))

解决方案

Assuming Your userTasks is an relationship You will find answer for Your case here. These is just how to sort but if You had required some WHERE conditions it is not so simple but neither it is hard.

I had to filter out some entities, the key to solve it was to create set/get method in entity class returning required set.

In my case it looks like this

/**
 * Set values
 *
 * @param ArrayCollection $values
 * @return Attribute
 */
public function setCustomValues($values)
{
    $result = $this->getNotCustomValues();
    foreach ($values as $value)
    {
        $value->setAttribute($this);
        $result->add($value);
    }
    $this->values = $result;

    return $this;
}

/**
 * Get values
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getCustomValues()
{
    $result = new ArrayCollection();
    foreach ($this->values as $value) {
        if($value->getCustom()) {
            $result->add($value);
        }
    }
    return $result;
}

And when creating form, name for a field is "customvalues" instead of "values" So my collection contains only values with custom field true.

这篇关于我可以使用表单构建器中的查询以symfony形式获取过滤收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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