doudui1850 2015-07-15 14:55
浏览 62

注册新用户后,在覆盖FormType后,新字段不会保留在数据库中

I am overriding the form type to register a user. All looks ok, but when I submit my form the new fields are not persisted in database.

I followed the documentation.

My ProfileType:

<?php
namespace Application\Sonata\UserBundle\Form\Type;

//use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

use Symfony\Component\OptionsResolver\OptionsResolverInterface;
//use Sonata\UserBundle\Model\UserInterface;

use Sonata\UserBundle\Form\Type\ProfileType as BaseType;

class ProfileType extends BaseType
{
    private $class;

    /**
     * @param string $class The User class name
     */
    public function __construct($class)
    {
        $this->class = $class;
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder
            ->add('username', null, array(
                'label'    => 'Pseudo',
                'required' => false
            ))
            ->add('firstname', null, array(
                'label'    => 'Prénom'
            ))
            ->add('lastname', null, array(
                'label'    => 'Nom'
            ))
            ->add('email', 'email', array(
                'label'    => 'Email'
            ))
            ->add('dateOfBirth', 'birthday', array(
                'label'    => 'Date d\'anniversaire',
                'required' => false,
                'data' => new \DateTime("01/01/1980")
            ))
            ->add('plainPassword', 'password', array(
                'label'    => 'Password'
            ))
            ->add('phone', null, array(
                'label'    => 'Téléphone',
                'required' => false
            ))
            ->add('adress', null, array(
                'label'    => 'Adresse',
                'required' => false
            ))
            ->add('zip', null, array(
                'label'    => 'Code postale',
                'required' => false
            ))
            ->add('city', null, array(
                'label'    => 'Ville',
                'required' => false
            ))
            ->add('newsletter', 'checkbox', array(
                'label'    => 'newsletter',
                'required' => false
            ))
            #hidden
            ->add('website', 'hidden', array(
                'label'    => 'website',
                'required' => false
            ))
            ->add('biography', 'hidden', array(
                'label'    => 'biography',
                'required' => false
            ))
            ->add('locale', 'hidden', array(
                'label'    => 'locale',
                'required' => false
            ))
            ->add('timezone', 'hidden', array(
                'label'    => 'Timezone',
                'required' => false
            ))
            ->add('gender', 'hidden', array(
                'label'    => 'Civilité',
                'required' => false
            ))
        ;
//        var_dump($builder);
    }

    /**
     * {@inheritdoc}
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Application\Sonata\UserBundle\Entity\User',
            'intention'  => 'profile',
            'label' => 'Edit Profile'
        ));
    }

//    public function getParent()
//    {
//        return 'fos_user_registration';
//    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'application_sonata_user_profile';
    }
}

My user class:

<?php
namespace Application\Sonata\UserBundle\Entity;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;
/**
 * Application\Sonata\UserBundle\Entity\User
 *
 * @ORM\Table(name="fos_user_user", indexes={@ORM\Index(name="search_idx", columns={"username", "email"})}))
 * @ORM\Entity()
 * @DoctrineAssert\UniqueEntity(fields={"username"}, message="username.already.exist" )
 * @DoctrineAssert\UniqueEntity(fields={"email"}, message="email.already.exist" )
 */
class User extends BaseUser
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="zip", type="string", length=255, nullable=true)
     */
    protected $zip;

    /**
     * @var string
     *
     * @ORM\Column(name="adress", type="text", nullable=true)
     */
    protected $adress;

    /**
     * @var string
     *
     * @ORM\Column(name="city", type="string", length=255, nullable=true)
     */
    protected $city;

    /**
     * @var boolean
     *
     * @ORM\Column(name="newsletter", type="boolean", nullable=true)
     */
    private $newsletter;


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

    /**
     * Set zip
     *
     * @param string $zip
     * @return FosUserUser
     */
    public function setZip($zip)
    {
        $this->zip = $zip;

        return $this;
    }

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

    /**
     * Set adress
     *
     * @param string $adress
     * @return FosUserUser
     */
    public function setAdress($adress)
    {
        $this->adress = $adress;

        return $this;
    }

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

    /**
     * Set city
     *
     * @param string $city
     * @return FosUserUser
     */
    public function setCity($city)
    {
        $this->city = $city;

        return $this;
    }

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

    /**
     * Set Newsletter
     *
     * @param boolean $newsletter
     * @return FosUserUser
     */
    public function setNewsletter($newsletter)
    {
        $this->newsletter = $newsletter;

        return $this;
    }

    /**
     * Get Newsletter
     *
     * @return boolean
     */
    public function getNewsletter()
    {
        return $this->newsletter;
    }
}

Thank you for your help.

  • 写回答

1条回答 默认 最新

  • drnysdnnb2909701 2015-07-15 18:05
    关注

    If you have a just created properties of the entity and you are using the metadata cache, the doctrine still doesn't aware about these new properties. Just try to clear the metadata cache.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?