0

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!

  • I asume you have the countries in the User Entity. Just use the relation from the user entity. – cptnk Apr 09 '14 at 09:24
  • Hello, thanks for the comment but I don't understand :( Yes, 'countries' is placed in User entity but I would love to set ObjectSelect's source as $zfcUserIdentity because it holds currently logged in user's allowed countries. – Mirko Bucko Apr 09 '14 at 10:17
  • @cptnk I forgot to add you in my comment above ... – Mirko Bucko Apr 09 '14 at 12:38
  • Im not entirely sure if the objectSelect is the best way to aproach this. Afterall you'll have a role dependencie in here 2 and a find by seems to be the only possible way to filter out stuff within the select. As I understand the Object select it just fills up the select trough the ORM via the ObjectRepository. I'd prob try and create my own form element(http://stackoverflow.com/questions/13776458/how-to-create-custom-form-element-in-zend-framework-2). – cptnk Apr 09 '14 at 13:06
  • 2
    If you already have the `Collection` with the entities you need then there is no point using the `DoctrineModule\Form\Element\ObjectSelect` as it will just load the collection again via the ObjectManager. Why not convert the `Collection` to a assoc array and set it as the `value_options` on a stardard select element? – AlexP Apr 09 '14 at 13:06

1 Answers1

0

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',
                    ),
                ), 
            ));
Karl Lorey
  • 1,536
  • 17
  • 21
  • This seems like almost the exact what I need but there's one more thing - how can I pass parameter(s) to getCountries() from the form?~ Basically if I wanted to have custom function with even more filtered countries? – Mirko Bucko Aug 19 '14 at 15:32