Can i use query in form builder to get filtered collection in symfony form
doctrine-orm, forms, php, symfony
Solution
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.
Problem
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'); }, )) ```