A
A
Alexander Evgenievich2015-01-30 18:05:06
symfony
Alexander Evgenievich, 2015-01-30 18:05:06

How to output select entity in collection field type?

There is an entity Brief and a Category (many-to-many relationship), on the Brief creation page there should be a list with categories. SELECT should not be multiple , so of course everything would work. Entity Brief has addCategory methods etc. The problem is that with this entry:

->add('categories', 'collection', array(
        'type'=>'entity',
        'options' => array(
        'class' => 'Aplication\UserBundle\Entity\Category',
        'property' => 'name',
        'query_builder' => function(EntityRepository $repository){
          return $repository->findAll();
        }),
        'by_reference' => false
      ))

outputs nothing at all. Those. even select does not return, just an empty space. I output via form_widget.
If you set the entity type instead of collection, it displays the list, but does not save it, because it waits for an ArrayCollection. It would, but if the select is with multiple="multiple" , which I don't need.
In general, the problem is that select does not display a list of categories.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Budanov Evgeniy, 2015-02-05
@zk-zeka

class Brief{
    .....
    public functioon getCategory()
    {
        return $this->categories;
    }
    
    public functioon setCategory( $category )
    {
        $this->categories->clear();   // если надо
        $this->addCategory( $category );
    }
}


class BriefType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        ......
        $builder->add( 'category', 'entity', array(
            .......
        ) );
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question