How to pass ArrayCollection to DoctrineModule\Form\Element\ObjectSelect

I tried pretty much everything I found by searching here and at Google too but still no luck.

I have User entity with ManytoMany relation with Countries, here is it:

/**
 * @var \Doctrine\Common\Collections\Collection
 * @ORM\ManyToMany(targetEntity="Admin\Entity\Country", cascade={"persist", "remove"})
 * @ORM\JoinTable(name="user_country_linker",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="country_id", referencedColumnName="id")}
 * )
 */
protected $countries;

Now I'm trying to display DoctrineModule\Form\Element\ObjectSelect with allowed/ assigned countries only. I do have this list available by calling $this->zfcUserAuthentication()->getIdentity()->getCountries().

Is there any way to pass this ArrayCollection to ObjectSelect form element?

$this->add(array(
        'name' => 'country',
        'type' => 'DoctrineModule\Form\Element\ObjectSelect',
        'options' => array(
            'label'             => 'Country',
            'object_manager'    => $em,
            'target_class'      => '\Admin\Entity\Country',
            'property'          => 'code',
            'find_method'       => array(
                'name' => 'findBy',
                'params' => array(
                    'criteria' => array(),
                    'orderBy' => array('id' => 'asc'),
                ),
            ),
            'column-size' => 'sm-10',
            'label_attributes' => array('class' => 'col-sm-2'),
            'help-block' => 'Select country where the entity is present'
        ),
        'attributes' => array(
            'required' => false
        )
    ));

Many thanks for the help, I really appreciate it!

Answers


How to fill a Dropdown in your controller is best described here: zf2 create select/drop down box and populate options in controller?. This is basically AlexP's solution.

If this is not what you are looking for, maybe the method described by this post can help you. At least it could help others like me that were looking for a solution like this: http://samsonasik.wordpress.com/2014/05/22/zend-framework-2-using-doctrinemoduleformelementobjectselect-and-custom-repository/

You basically create a custom reposity which holds a custom query to retrieve possible solutions:

namespace Your\Repository;


    use Doctrine\ORM\EntityRepository;

    class CountriesRepository extends EntityRepository
    {
        public function getPossibleCountries()
        {
            $querybuilder = $this->_em
                                 ->getRepository($this->getEntityName())
                                 ->createQueryBuilder('c');
            return $querybuilder->select('c')//... define your query here
                        ->getQuery()->getResult();
        }
    }

You can then refer to that method in your ObjectSelect:

            $this->add(array(
               'name' => 'continent',
               'type' => 'DoctrineModule\Form\Element\ObjectSelect',
               'options' => array(
                    'object_manager'     => $this->entityManager,
                    'target_class'       => 'Your\Entity\User',
                    'property' => 'contries',
                    'is_method' => true,
                    'find_method'        => array(
                        'name'   => 'getCountries',
                    ),
                ), 
            ));

Need Your Help

Long app startup time with Android Plugin for Gradle 2.2.0

android android-gradle

Does anyone notice the app takes much longer time to start up using Android Plugin for Gradle 2.2.0?