title: Symfony 2 Embedded Forms
body: <p>I am embedding a form type into another form type like so </p>
  
  <pre><code>$builder-&gt;add('parent', new \Company\Bundle\Form\UserObjects\AParentType);
  </code></pre>
  
  <p>However when I try and bind the request to the form</p>
  
  <pre><code>if($request-&gt;getMethod() == 'POST') {
        $form-&gt;bindRequest($request);
  }
  </code></pre>
  
  <p>I get the error</p>
  
  <blockquote>
    <p>Catchable Fatal Error: Argument 1 passed to Company\Bundle\Entity\UserObjects\User::setParent() must be an instance of  Company\Bundle\Entity\UserObjects\AParent, array given, called in /Volumes/Media/Symfony/vendor/symfony/src/Symfony/Component/Form/Util/PropertyPath.php on line 346 and defined in /Volumes/Media/Symfony/src/Company/Bundle/Entity/UserObjects/User.php line 771</p>
  </blockquote>
  
  <p>It seems like the form is passing the "AParent" object as an array instead of as an entity. Any ideas?</p>
  
  <p><strong>Edited</strong></p>
  
  <p>User.php</p>
  
  <pre><code>    &lt;?php
      // src/Company/Bundle/Entity/UserObjects/Users.php
  
      namespace Company\Bundle\Entity\UserObjects;
  
      use Symfony\Component\Security\Core\User\UserInterface;
  
      use Doctrine\ORM\Mapping as ORM;
  
      /**
       * @ORM\Entity(repositoryClass="Company\Bundle\Repository\UserObjects\UserRepository")
       * @ORM\Table(name="user")
       * @ORM\HasLifecycleCallbacks()
       */
      class User implements UserInterface, \Serializable
      {
           /**
           * @ORM\Id
           * @ORM\Column(type="integer")
           * @ORM\GeneratedValue(strategy="AUTO")
           */
          protected $id;
  
           /**
           * @ORM\Column(type="string")
           */
          protected $username;
  
           /**
           * @ORM\Column(type="string")
           */
          protected $password;
  
           /**
           * @ORM\Column(type="string")
           */
          protected $securityQuestion;
  
           /**
           * @ORM\Column(type="string")
           */
          protected $securityAnswer;
  
           /**
           * @ORM\Column(type="string")
           */
          protected $salt;
  
  
          /**
           * @ORM\OneToOne(targetEntity="AParent", inversedBy="user")
           */
          private $parent;
  
  
  
  
  
  
  
          public function serialize()  
          {  
              return serialize(array(  
                  'username'    =&gt; $this-&gt;getUsername(),  
                  'password'    =&gt; $this-&gt;getPassword(),  
                  'salt'        =&gt; $this-&gt;getSalt(),  
                  'roles'       =&gt; $this-&gt;getRoles(),
              ));  
          } 
  
          public function unserialize($serializedData)  
          {  
              $unserializedData     = unserialize($serializedData);  
  
              $this-&gt;setUsername(isset($unserializedData['username']) ? $unserializedData['username'] : null);  
              $this-&gt;setPassword(isset($unserializedData['password']) ? $unserializedData['password'] : null);  
              $this-&gt;setSalt(isset($unserializedData['salt']) ? $unserializedData['salt'] : null); 
          }  
  
          public function getRoles()
          {
              return array('ROLE_USER');
          }
  
  
  
  
          public function eraseCredentials()
          {
              return false;
          }
  
           public function equals(UserInterface $user)
          {
                 if ($user-&gt;getUsername() != $this-&gt;getUsername()) {
                      return false;
                  }
                  if ($user-&gt;getEmail() != $this-&gt;getEmail()) {
                      return false;
                  }
                  return true;
          }
  
          /**
           * Get password
           *
           * @return string 
           */
          public function getPassword()
          {
              return $this-&gt;password;
          }
  
          /**
           * Get salt
           *
           * @return string 
           */
          public function getSalt()
          {
              return $this-&gt;salt;
          }
  
          public function getUsername()
          {
              return $this-&gt;username;
          }
  
          /**
           * Get id
           *
           * @return integer 
           */
          public function getId()
          {
              return $this-&gt;id;
          }
  
          /**
           * Set username
           *
           * @param string $username
           */
          public function setUsername($username)
          {
              $this-&gt;username = $username;
          }
  
          /**
           * Set password
           *
           * @param string $password
           */
          public function setPassword($password)
          {
              $this-&gt;password = $password;
          }
  
          /**
           * Set securityQuestion
           *
           * @param string $securityQuestion
           */
          public function setSecurityQuestion($securityQuestion)
          {
              $this-&gt;securityQuestion = $securityQuestion;
          }
  
          /**
           * Get securityQuestion
           *
           * @return string 
           */
          public function getSecurityQuestion()
          {
              return $this-&gt;securityQuestion;
          }
  
          /**
           * Set securityAnswer
           *
           * @param string $securityAnswer
           */
          public function setSecurityAnswer($securityAnswer)
          {
              $this-&gt;securityAnswer = $securityAnswer;
          }
  
          /**
           * Get securityAnswer
           *
           * @return string 
           */
          public function getSecurityAnswer()
          {
              return $this-&gt;securityAnswer;
          }
  
          /**
           * Set salt
           *
           * @param string $salt
           */
          public function setSalt($salt)
          {
              $this-&gt;salt = $salt;
          }
  
  
  
  
          /**
           * Set parent
           *
           * @param Company\Bundle\Entity\UserObjects\AParent $parent
           */
          public function setParent(\DABSquared\ProjectGradesBundle\Entity\UserObjects\AParent $parent)
          {
              $this-&gt;parent = $parent;
              if($parent != null) {
                  $parent-&gt;setUser($this);
              }
          }
  
          /**
           * Get parent
           *
           * @return Company\Bundle\Entity\UserObjects\AParent 
           */
          public function getParent()
          {
              return $this-&gt;parent;
          }
  
  
          }
          public function __construct()
          {
          }
  
      }
  </code></pre>
  
  <p>AParent.php</p>
  
  <pre><code>    &lt;?php
      // src/Company/Bundle/Entity/UserObjects/AParent.php
  
      namespace Company\Bundle\Entity\UserObjects;
  
      use Doctrine\ORM\Mapping as ORM;
  
  
  
      /**
       * @ORM\Entity(repositoryClass="Company\Bundle\Repository\UserObjects\AParentRepository")
       * @ORM\Table(name="parents")
       * @ORM\HasLifecycleCallbacks()
       */
      class AParent
      {
          /**
           * @ORM\Id
           * @ORM\Column(type="integer")
           * @ORM\GeneratedValue(strategy="AUTO")
           */
          protected $id;
  
  
          /**
           * @ORM\OneToOne(targetEntity="User", inversedBy="parent")
           */
           private $user;
  
                      /**
                       * @ORM\Column(type="string")
                       */
                      protected $zipCode;
  
  
          public function __construct()
          {
  
          }
  
          /**
           * Get id
           *
           * @return integer 
           */
          public function getId()
          {
              return $this-&gt;id;
          }
  
          /**
           * Set user
           *
           * @param Company\Bundle\Entity\UserObjects\User $user
           */
          public function setUser(\Company\Bundle\Entity\UserObjects\User $user)
          {
              $this-&gt;user = $user;
          }
  
          /**
           * Get user
           *
           * @return Company\Bundle\Entity\UserObjects\User 
           */
          public function getUser()
          {
              return $this-&gt;user;
          }
  
                      /**
           * Set zipCode
           *
           * @param string $zipCode
           */
          public function setZipCode($zipCode)
          {
              $this-&gt;zipCode = $zipCode;
          }
  
          /**
           * Get zipCode
           *
           * @return string 
           */
          public function getZipCode()
          {
              return $this-&gt;zipCode;
          }
  
  
  
      }
  </code></pre>
  
  <p>UserType.php</p>
  
  <pre><code>    &lt;?php
  
      namespace Company\Bundle\Form;
  
      use Symfony\Component\Form\AbstractType;
      use Symfony\Component\Form\FormBuilder;
  
      use Doctrine\ORM\EntityRepository;
  
      class UserType extends AbstractType
      {
          public function buildForm(FormBuilder $builder, array $options)
          {
              $builder
                  -&gt;add('username',null,array('label' =&gt; 'Username:'))
                  -&gt;add('password',null,array('label' =&gt; 'Password:'))
                  -&gt;add('securityQuestion',null,array('label' =&gt; 'Security Question:'))
                  -&gt;add('securityAnswer',null,array('label' =&gt; 'Security Answer:'))
                  -&gt;add('parent', new \Company\Bundle\Form\UserObjects\AParentType);
  
          }
  
          public function getName()
          {
              return 'usertype';
          }
      }
  </code></pre>
  
  <p>AParentType.php</p>
  
  <pre><code>    &lt;?php
  
      namespace Company\Bundle\Form\UserObjects;
  
      use Symfony\Component\Form\AbstractType;
      use Symfony\Component\Form\FormBuilder;
  
      class AParentType extends AbstractType
      {
          public function buildForm(FormBuilder $builder, array $options)
          {
              $builder
                  -&gt;add('zipCode',null,array('label' =&gt; 'Zip Code:'));
          }
  
          public function getName()
          {
              return 'aparenttype';
          }
      }
  </code></pre>