title: SonataAdminBundle embedded forms sonata_admin_type issue
body: <p>I've got two entities, Quiz and QuizQuestion, with manyToMany one-sided relation. I'd like to embed questions form in quiz form. I'm on 2.0 branches. I am able to get <code>sonata_type_model</code> to work, getting list of id's in a dropdown, and working "add" button. However, I'm getting an error when trying to use <code>sonata_type_admin</code>:</p>
  
  <pre><code>Neither property "title" nor method "getTitle()" nor method "isTitle()" exists in class "Doctrine\ORM\PersistentCollection"
  500 Internal Server Error - InvalidPropertyException
  </code></pre>
  
  <p>Here's my Quiz entity:</p>
  
  <pre><code>    &lt;?php 
  
  namespace Some\SiteBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  
  
  /**
   * Some\SiteBundle\Entity\Quiz
   * @ORM\Table(name="quiz")
   * @ORM\Entity(repositoryClass="Some\SiteBundle\Entity\QuizRepository")
  
   */
  class Quiz
  {
      /**
       * @var integer $id
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
  
  
      /**
       * @ORM\Column(type="datetime", name="created_at")
       * 
       * @var DateTime $createdAt
       */
      protected $createdAt;
  
  
      /**
       * @var string $title
       *
       * @ORM\Column(name="title", type="string", length=255)
       */
      private $title;
  
  
      /**
       * @var string $body
       *
       * @ORM\Column(name="body", type="text")
       */
      private $body;
  
  
    /**
      * @var QuizQuestion questions
       * @ORM\ManyToMany(targetEntity="QuizQuestion", cascade={"persist", "remove"} )
       **/
      protected $questions;
  
  
      public function __construct() {
          $this-&gt;questions = new ArrayCollection();
          $this-&gt;createdAt = new \DateTime();
      }
  
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set title
       *
       * @param string $title
       */
      public function setTitle($title)
      {
          $this-&gt;title = $title;
      }
  
      /**
       * Get title
       *
       * @return string 
       */
      public function getTitle()
      {
          return $this-&gt;title;
      }
  
  
      /**
       * Get quiz body.
       * 
       * @return string body
       */
      public function getBody()
      {
          return $this-&gt;body;
      }
  
      /**
       * Sets body
       * 
       * @param string $value body
       */
      public function setBody($body)
      {
          $this-&gt;body = $body;
      }
  
  
  
      /**
       * Gets an object representing the date and time the quiz was created.
       * 
       * @return DateTime A DateTime object
       */
      public function getCreatedAt()
      {
          return $this-&gt;createdAt;
      }   
  
  
  
      /**
       * Add questions
       *
       * @param Some\SiteBundle\Entity\QuizQuestion $questions
       */
      public function addQuestion(\Some\SiteBundle\Entity\QuizQuestion $question)
      {
          $this-&gt;questions[] = $question;
      }
  
      /**
       * set question
       *
       * @param Some\SiteBundle\Entity\QuizQuestion $questions
       */
      public function setQuestion(\Some\SiteBundle\Entity\QuizQuestion $question)
      {
          foreach ($this-&gt;questions as $doc) {
              $this-&gt;questions-&gt;removeElement($doc);
          }
          $this-&gt;questions[] = $question;
      }
  
      /**
       * Get questions
       *
       * @return Doctrine\Common\Collections\Collection 
       */
      public function getQuestions()
      {
          return $this-&gt;questions;
      }
  
  
      /**
       * @ORM\PrePersist
       */
      public function beforePersist()
      {
          //$this-&gt;setCreatedAt(new \DateTime());        
          //$this-&gt;setModifiedAt(new \DateTime());
      }
  
  
        public function __toString()
    {
      return 'Quiz';
    }
  }
  </code></pre>
  
  <p>And QuizQuestion entity:</p>
  
  <pre><code>&lt;?php 
  
  namespace Some\SiteBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  
  
  /**
   * Some\SiteBundle\Entity\QuizQuestion
   * @ORM\Table(name="quiz_question")
   * @ORM\Entity
   */
  class QuizQuestion
  {
      /**
       * @var integer $id
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
  
  
      /**
       * @ORM\Column(type="datetime", name="created_at")
       * 
       * @var DateTime $createdAt
       */
      protected $createdAt;
  
  
      /**
       * @var string $title
       *
       * @ORM\Column(name="title", type="string", length=255)
       */
      private $title;
  
  
      /**
       * @var string $body
       *
       * @ORM\Column(name="body", type="text")
       */
      private $body;
  
       /**
       * @var string $answer1
       *
       * @ORM\Column(name="answer1", type="text")
       */
      private $answer1;
  
       /**
       * @var string $answer2
       *
       * @ORM\Column(name="answer2", type="text")
       */
      private $answer2;
  
        /**
       * @var string $answer3
       *
       * @ORM\Column(name="answer3", type="text")
       */
      private $answer3;
  
       /**
       * @var string $answer4
       *
       * @ORM\Column(name="answer4", type="text")
       */
      private $answer4;
  
      /**
       * @var string $correctAnswer
       *
       * @ORM\Column(name="correct_answer", type="integer", length="1")
       */
      private $correctAnswer;
  
  
  
  
      public function __construct() {
          $this-&gt;createdAt = new \DateTime();
      }
  
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set title
       *
       * @param string $title
       */
      public function setTitle($title)
      {
          $this-&gt;title = $title;
      }
  
      /**
       * Get title
       *
       * @return string 
       */
      public function getTitle()
      {
          return $this-&gt;title;
      }
  
  
      /**
       * Get question body.
       * 
       * @return string body
       */
      public function getBody()
      {
          return $this-&gt;body;
      }
  
      /**
       * Sets body
       * 
       * @param string $value body
       */
      public function setBody($body)
      {
          $this-&gt;body = $body;
      }
  
  
      /**
       * Get question answer1.
       * 
       * @return string answer1
       */
      public function getAnswer1()
      {
          return $this-&gt;answer1;
      }
  
      /**
       * Sets answer1
       * 
       * @param string $value answer1
       */
      public function setAnswer1($answer1)
      {
          $this-&gt;answer1 = $answer1;
      }
  
          /**
       * Get question answer2.
       * 
       * @return string answer2
       */
      public function getAnswer2()
      {
          return $this-&gt;answer2;
      }
  
      /**
       * Sets answer2
       * 
       * @param string $value answer2
       */
      public function setAnswer2($answer2)
      {
          $this-&gt;answer2 = $answer2;
      }
  
          /**
       * Get question answer3.
       * 
       * @return string answer3
       */
      public function getAnswer3()
      {
          return $this-&gt;answer3;
      }
  
      /**
       * Sets answer3
       * 
       * @param string $value answer3
       */
      public function setAnswer3($answer3)
      {
          $this-&gt;answer3 = $answer3;
      }
  
          /**
       * Get question answer4.
       * 
       * @return string answer4
       */
      public function getAnswer4()
      {
          return $this-&gt;answer4;
      }
  
      /**
       * Sets answer4
       * 
       * @param string $value answer4
       */
      public function setAnswer4($answer4)
      {
          $this-&gt;answer4 = $answer4;
      }
  
       /**
       * Get question correctAnswer.
       * 
       * @return string correctAnswer
       */
      public function getCorrectAnswer()
      {
          return $this-&gt;correctAnswer;
      }
  
      /**
       * Sets answer1
       * 
       * @param string $value correctAnswer
       */
      public function setCorrectAnswer($correctAnswer)
      {
          $this-&gt;correctAnswer = $correctAnswer;
      }
  
      /**
       * Gets an object representing the date and time the question was created.
       * 
       * @return DateTime A DateTime object
       */
      public function getCreatedAt()
      {
          return $this-&gt;createdAt;
      }   
  
  
  
  
    public function __toString()
    {
      return $this-&gt;title;
    }
  }
  </code></pre>
  
  <p>And relevant admin classes. QuizAdmin first:</p>
  
  <pre><code>    &lt;?php 
  
  namespace Some\SiteBundle;
  
  use Some\SiteBundle\Form\QuizQuestionType;
  use Sonata\AdminBundle\Admin\Admin;
  use Sonata\AdminBundle\Datagrid\ListMapper;
  use Sonata\AdminBundle\Datagrid\DatagridMapper;
  use Sonata\AdminBundle\Validator\ErrorElement;
  use Sonata\AdminBundle\Form\FormMapper;
  use Sonata\AdminBundle\Show\ShowMapper;
  use Symfony\Component\HttpFoundation\File\File;
  use Symfony\Component\HttpFoundation\Request;
  
  class QuizAdmin extends Admin
  {
  
  
      protected function configureFormFields(FormMapper $formMapper)
      {
          $formMapper
                -&gt;add('title', NULL, array('label' =&gt; 'tytuł:'))
                -&gt;add('body', NULL, array('label' =&gt; 'opis:', 'required' =&gt; false, 'attr' =&gt; array(
                    'class' =&gt; 'tinymce', 'data-theme' =&gt; 'simple')
                ))
                -&gt;add('questions', 'sonata_type_admin', array(), array('required' =&gt; false, 'edit' =&gt; 'inline'));
                //-&gt;add('categories', NULL, array('label' =&gt; 'kategorie:'))
          ;
      }
  
      protected function configureDatagridFilters(DatagridMapper $datagridMapper)
      {
          $datagridMapper
              -&gt;add('title')
              -&gt;add('body')
          ;
      }
  
      protected function configureListFields(ListMapper $listMapper)
      {
          $listMapper
              -&gt;addIdentifier('id')
              -&gt;add('title')
              -&gt;add('_action', 'actions', array(
                  'actions' =&gt; array(
                      'view' =&gt; array(),
                      'edit' =&gt; array(),
                  )
              ))
              //-&gt;add('body')
          ;
  
      }
  
      public function validate(ErrorElement $errorElement, $object)
      {
          $errorElement
              -&gt;with('title')
                  -&gt;assertMinLength(array('limit' =&gt; 2))
              -&gt;end()
          ;
      }
  
  
  
  }
  </code></pre>
  
  <p>And QuizQuestionAdmin:</p>
  
  <pre><code>    &lt;?php 
  
  namespace Some\SiteBundle;
  
  use Some\SiteBundle\Form\QuizQuestionType;
  use Sonata\AdminBundle\Admin\Admin;
  use Sonata\AdminBundle\Datagrid\ListMapper;
  use Sonata\AdminBundle\Datagrid\DatagridMapper;
  use Sonata\AdminBundle\Validator\ErrorElement;
  use Sonata\AdminBundle\Form\FormMapper;
  use Sonata\AdminBundle\Show\ShowMapper;
  use Symfony\Component\HttpFoundation\File\File;
  use Symfony\Component\HttpFoundation\Request;
  
  class QuizAdmin extends Admin
  {
  
  
      protected function configureFormFields(FormMapper $formMapper)
      {
          $formMapper
                -&gt;add('title', NULL, array('label' =&gt; 'tytuł:'))
                -&gt;add('body', NULL, array('label' =&gt; 'opis:', 'required' =&gt; false, 'attr' =&gt; array(
                    'class' =&gt; 'tinymce', 'data-theme' =&gt; 'simple')
                ))
                -&gt;add('questions', 'sonata_type_admin', array(), array('required' =&gt; false, 'edit' =&gt; 'inline'));
                //-&gt;add('categories', NULL, array('label' =&gt; 'kategorie:'))
          ;
      }
  
      protected function configureDatagridFilters(DatagridMapper $datagridMapper)
      {
          $datagridMapper
              -&gt;add('title')
              -&gt;add('body')
          ;
      }
  
      protected function configureListFields(ListMapper $listMapper)
      {
          $listMapper
              -&gt;addIdentifier('id')
              -&gt;add('title')
              -&gt;add('_action', 'actions', array(
                  'actions' =&gt; array(
                      'view' =&gt; array(),
                      'edit' =&gt; array(),
                  )
              ))
              //-&gt;add('body')
          ;
  
      }
  
      public function validate(ErrorElement $errorElement, $object)
      {
          $errorElement
              -&gt;with('title')
                  -&gt;assertMinLength(array('limit' =&gt; 2))
              -&gt;end()
          ;
      }
  
  
  
  }
  </code></pre>
  
  <p>I tried to register quizQuestion as a service, but then I get <code>Expected argument of type "Festus\SiteBundle\Entity\QuizQuestion", "Doctrine\ORM\PersistentCollection" given
  500 Internal Server Error - UnexpectedTypeException</code></p>
  
  <p>Couldn't find any solution after few hours of looking things up...</p>