Symfony 2嵌入式表格

时间:2011-11-07 03:39:25

标签: php forms symfony symfony-forms

我正在将表单类型嵌入到另一种表单类型中,如此

$builder->add('parent', new \Company\Bundle\Form\UserObjects\AParentType);

然而,当我尝试将请求绑定到表单

if($request->getMethod() == 'POST') {
      $form->bindRequest($request);
}

我收到错误

  

捕获致命错误:传递给Company \ Bundle \ Entity \ UserObjects \ User :: setParent()的参数1必须是Company \ Bundle \ Entity \ UserObjects \ _AParent的实例,给定数组,在/ Volumes / Media /中调用第346行的Symfony / vendor / symfony / src / Symfony / Component / Form / Util / PropertyPath.php,在/Volumes/Media/Symfony/src/Company/Bundle/Entity/UserObjects/User.php第771行中定义

似乎表单将“AParent”对象作为数组而不是实体传递。有什么想法吗?

被修改

user.php的

    <?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'    => $this->getUsername(),  
                'password'    => $this->getPassword(),  
                'salt'        => $this->getSalt(),  
                'roles'       => $this->getRoles(),
            ));  
        } 

        public function unserialize($serializedData)  
        {  
            $unserializedData     = unserialize($serializedData);  

            $this->setUsername(isset($unserializedData['username']) ? $unserializedData['username'] : null);  
            $this->setPassword(isset($unserializedData['password']) ? $unserializedData['password'] : null);  
            $this->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->getUsername() != $this->getUsername()) {
                    return false;
                }
                if ($user->getEmail() != $this->getEmail()) {
                    return false;
                }
                return true;
        }

        /**
         * Get password
         *
         * @return string 
         */
        public function getPassword()
        {
            return $this->password;
        }

        /**
         * Get salt
         *
         * @return string 
         */
        public function getSalt()
        {
            return $this->salt;
        }

        public function getUsername()
        {
            return $this->username;
        }

        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }

        /**
         * Set username
         *
         * @param string $username
         */
        public function setUsername($username)
        {
            $this->username = $username;
        }

        /**
         * Set password
         *
         * @param string $password
         */
        public function setPassword($password)
        {
            $this->password = $password;
        }

        /**
         * Set securityQuestion
         *
         * @param string $securityQuestion
         */
        public function setSecurityQuestion($securityQuestion)
        {
            $this->securityQuestion = $securityQuestion;
        }

        /**
         * Get securityQuestion
         *
         * @return string 
         */
        public function getSecurityQuestion()
        {
            return $this->securityQuestion;
        }

        /**
         * Set securityAnswer
         *
         * @param string $securityAnswer
         */
        public function setSecurityAnswer($securityAnswer)
        {
            $this->securityAnswer = $securityAnswer;
        }

        /**
         * Get securityAnswer
         *
         * @return string 
         */
        public function getSecurityAnswer()
        {
            return $this->securityAnswer;
        }

        /**
         * Set salt
         *
         * @param string $salt
         */
        public function setSalt($salt)
        {
            $this->salt = $salt;
        }




        /**
         * Set parent
         *
         * @param Company\Bundle\Entity\UserObjects\AParent $parent
         */
        public function setParent(\DABSquared\ProjectGradesBundle\Entity\UserObjects\AParent $parent)
        {
            $this->parent = $parent;
            if($parent != null) {
                $parent->setUser($this);
            }
        }

        /**
         * Get parent
         *
         * @return Company\Bundle\Entity\UserObjects\AParent 
         */
        public function getParent()
        {
            return $this->parent;
        }


        }
        public function __construct()
        {
        }

    }

AParent.php

    <?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->id;
        }

        /**
         * Set user
         *
         * @param Company\Bundle\Entity\UserObjects\User $user
         */
        public function setUser(\Company\Bundle\Entity\UserObjects\User $user)
        {
            $this->user = $user;
        }

        /**
         * Get user
         *
         * @return Company\Bundle\Entity\UserObjects\User 
         */
        public function getUser()
        {
            return $this->user;
        }

                    /**
         * Set zipCode
         *
         * @param string $zipCode
         */
        public function setZipCode($zipCode)
        {
            $this->zipCode = $zipCode;
        }

        /**
         * Get zipCode
         *
         * @return string 
         */
        public function getZipCode()
        {
            return $this->zipCode;
        }



    }

UserType.php

    <?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
                ->add('username',null,array('label' => 'Username:'))
                ->add('password',null,array('label' => 'Password:'))
                ->add('securityQuestion',null,array('label' => 'Security Question:'))
                ->add('securityAnswer',null,array('label' => 'Security Answer:'))
                ->add('parent', new \Company\Bundle\Form\UserObjects\AParentType);

        }

        public function getName()
        {
            return 'usertype';
        }
    }

AParentType.php

    <?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
                ->add('zipCode',null,array('label' => 'Zip Code:'));
        }

        public function getName()
        {
            return 'aparenttype';
        }
    }

1 个答案:

答案 0 :(得分:8)

您尚未在AParentType表单中设置数据类。你这样做:

public function getDefaultOptions(array $options)
{
    return array(
        'data_class' => 'Project\MyBundle\Entity\AParent',
    );
}

对于Symfony的更高版本,您需要使用此代码:

use Symfony\Component\OptionsResolver\OptionsResolverInterface;
//...

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Project\MyBundle\Entity\AParent',
    ));
}

替换它:

->add('parent', new \Company\Bundle\Form\UserObjects\AParentType);

有了这个:

->add('parent', new AParentType());

因为您已经包含了命名空间。

如果它是一对一的,那么,在您的表单模板中,您可以:

{{ form_row(form.parent.zipCode) }}

希望这应该有效。您的实体定义看起来很好: - )