@H_
404_0@
在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',))
假设你的userTasks是一个关系你会找到你的案例
here的答案.这些只是如何排序,但如果你需要一些WHERE条件,它不是那么简单,但也不难.
我必须过滤掉一些实体,解决的关键是在实体类中创建set / get方法返回所需的集合.
在我的情况下,看起来像这样
/**
* 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;
}
而当创建表单时,字段的名称是“customvalues”而不是“values”所以我的集合只包含自定义字段true的值.