title: ZF2 Doctrine get manytomany relations with objectSelect
body: <p>I have multiple users, with multiple stores in a many to many relational database. Every user has multiple stores attached to them.</p>
  
  <p>Now, i want to load all the storenames from a logged in user in a select form. </p>
  
  <p>How can i do this?</p>
  
  <p>My user entity:</p>
  
  <pre><code>namespace Application\Entity;
  
  use BjyAuthorize\Provider\Role\ProviderInterface;
  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\Common\Collections\Collection;
  use Doctrine\ORM\Mapping as ORM;
  use ZfcUser\Entity\UserInterface;
  
  /**
   * An example of how to implement a role aware user entity.
   *
   * @ORM\Entity
   * @ORM\Table(name="users")
   * @ORM\Entity(repositoryClass="Application\Repositories\UserRepository")
   *
   */
  class User implements UserInterface, ProviderInterface
  {
      /**
       * @var int
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;
  
      /**
       * @var string
       * @ORM\Column(type="string", length=255, unique=true, nullable=true)
       */
      protected $username;
  
      /**
       * @var string
       * @ORM\Column(type="string", unique=true,  length=255)
       */
      protected $email;
  
      /**
       * @var string
       * @ORM\Column(type="string", length=50, nullable=true)
       */
      protected $displayName;
  
      /**
       * @var string
       * @ORM\Column(type="string", length=128)
       */
      protected $password;
  
      /**
       * @var int
       */
      protected $state;
  
      /**
       * @var \Doctrine\Common\Collections\Collection
       * @ORM\ManyToMany(targetEntity="Application\Entity\Role")
       * @ORM\JoinTable(name="user_role_linker",
       *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
       *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
       * )
       */
      protected $roles;
  
      /**
           * @var \Doctrine\Common\Collections\Collection
           * @ORM\ManyToMany(targetEntity="Application\Entity\Store")
           * @ORM\JoinTable(name="user_store_linker",
           *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
           *      inverseJoinColumns={@ORM\JoinColumn(name="store_id", referencedColumnName="id")}
           * )
       */
      protected $stores;
  
  
      /**
       * Initialies the roles variable.
       */
      public function __construct()
      {
          $this-&gt;roles = new ArrayCollection();
          $this-&gt;stores = new ArrayCollection();
      }
  
      /**
       * Get id.
       *
       * @return int
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set id.
       *
       * @param int $id
       *
       * @return void
       */
      public function setId($id)
      {
          $this-&gt;id = (int) $id;
      }
  
      /**
       * Get username.
       *
       * @return string
       */
      public function getUsername()
      {
          return $this-&gt;username;
      }
  
      /**
       * Set username.
       *
       * @param string $username
       *
       * @return void
       */
      public function setUsername($username)
      {
          $this-&gt;username = $username;
      }
  
      /**
       * Get email.
       *
       * @return string
       */
      public function getEmail()
      {
          return $this-&gt;email;
      }
  
      /**
       * Set email.
       *
       * @param string $email
       *
       * @return void
       */
      public function setEmail($email)
      {
          $this-&gt;email = $email;
      }
  
      /**
       * Get displayName.
       *
       * @return string
       */
      public function getDisplayName()
      {
          return $this-&gt;displayName;
      }
  
      /**
       * Set displayName.
       *
       * @param string $displayName
       *
       * @return void
       */
      public function setDisplayName($displayName)
      {
          $this-&gt;displayName = $displayName;
      }
  
      /**
       * Get password.
       *
       * @return string
       */
      public function getPassword()
      {
          return $this-&gt;password;
      }
  
      /**
       * Set password.
       *
       * @param string $password
       *
       * @return void
       */
      public function setPassword($password)
      {
          $this-&gt;password = $password;
      }
  
      /**
       * Get state.
       *
       * @return int
       */
      public function getState()
      {
          return $this-&gt;state;
      }
  
      /**
       * Set state.
       *
       * @param int $state
       *
       * @return void
       */
      public function setState($state)
      {
          $this-&gt;state = $state;
      }
  
      /**
       * Get role.
       *
       * @return array
       */
      public function getRoles()
      {
          return $this-&gt;roles-&gt;getValues();
      }
  
      /**
       * Add a role to the user.
       *
       * @param Role $role
       *
       * @return void
       */
      public function addRole($role)
      {
          $this-&gt;roles[] = $role;
      }
  
      /**
       * Get store.
       *
       * @return array
       */
      public function getStores()
      {
          return $this-&gt;stores;
      }
  
      /**
       * Get store.
       *
       * @return array
       */
      public function getStore($id)
      {
          return $this-&gt;stores[$id]-&gt;getValues();
      }
  
      /**
       * Add a store to the user.
       *
       * @param Role $store
       *
       * @return void
       */
      public function addStore($store)
      {
          $this-&gt;stores[] = $store;
      }
  }
  </code></pre>
  
  <p>My store Entity:</p>
  
  <pre><code>namespace Application\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * An example entity that represents a store.
   *
   * @ORM\Entity
   * @ORM\Table(name="store")
   *
   */
  class Store
  {
      /**
       * @var int
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;
  
      /**
       * @var string
       * @ORM\Column(type="string", name="storeName", length=255, unique=true, nullable=true)
       */
      protected $storeName;
  
      /**
       * @var \Doctrine\Common\Collections\Collection
       * @ORM\ManyToMany(targetEntity="Application\Entity\Product" )
       */
      protected $products;
  
      /**
       * Initialies the roles variable.
       */
      public function __construct()
      {
          $this-&gt;products = new ArrayCollection();
      }
  
      /**
       * Get the id.
       *
       * @return int
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set the id.
       *
       * @param int $id
       *
       * @return void
       */
      public function setId($id)
      {
          $this-&gt;id = (int)$id;
      }
  
      /**
       * Get the store id.
       *
       * @return string
       */
      public function getStoreName()
      {
          return $this-&gt;storeName;
      }
  
      /**
       * Set the store id.
       *
       * @param string $storeName
       *
       * @return void
       */
      public function setStoreName($storeName)
      {
          $this-&gt;storeName = (string) $storeName;
      }
  
      /**
       * Get product.
       *
       * @return array
       */
      public function getProducts()
      {
          return $this-&gt;products-&gt;getValues();
      }
  
      /**
       * Add a product to the user.
       *
       * @param Role $product
       *
       * @return void
       */
      public function addProduct($products)
      {
          $this-&gt;products[] = $products;
      }
  
  }
  </code></pre>
  
  <p>My form:</p>
  
  <pre><code>    $this-&gt;add(array(
          'type' =&gt; 'DoctrineModule\Form\Element\ObjectSelect',
          'name' =&gt; 'stores',
          'attributes' =&gt; array(
              'multiple' =&gt; true,
          ),
          'options' =&gt; array(
              'object_manager' =&gt; $objectManager,
              'target_class'   =&gt; 'Application\Entity\User',
              'label' =&gt; 'Selecteer winkel',
              'column-size' =&gt; 'sm-9',
              'label_attributes' =&gt; array('class' =&gt; 'col-sm-3 control-label'),
              'property'       =&gt; 'stores',
              'find_method' =&gt; array(
                  'name' =&gt; 'findBy',
                  'params' =&gt; array(
                      'criteria' =&gt; array('id' =&gt; $userid)
                  ),
              ),
              'is_method'      =&gt; true,
          ),
      ));
  </code></pre>
  
  <p>I'm getting this message:</p>
  
  <pre><code>    File:
       zend/vendor/zendframework/zendframework/library/Zend/View/Helper/Escaper/AbstractHelper.php:70
  Message:
  Object provided to Escape helper, but flags do not allow recursion
  </code></pre>
  
  <p>Anyone?</p>