FOSUserBundle表单扩展

我已经成功地重写了FOSUserbundle的User类,添加了一堆属性。 但是当我试图添加'类型'属性,你可以看到下面,我有以下例外:

无论属性“类型”还是方法“addTyp()”/“removeTyp()”,“addType()”/“removeType()”,“setTypes()”,“types()”,“__set )“或”__call()“存在并且在”Advertproject UserBundle Entity User“类中具有公共访问权限。

类型是一个独立的实体,与我的custumized实体/用户具有ManyToMany关系。

这个想法是在主窗体中插入一个单选按钮窗体。 我已经阅读了许多类似的错误消息提供的答案,但没有任何工作。 其实我的情况与此相似(答案由@Mick提供)。 但提供的建议不适用于我的情况,因为我确实在那里推荐了这些建议。 任何人都可以帮我解决这个问题吗? 请注意,表单正在使用单选按钮表单正确呈现,并且在我点击提交按钮后返回异常。

Advertproject / UserBundle /实体/用户:

namespace AdvertprojectUserBundleEntity;

use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
use FOSUserBundleModelUser as BaseUser;
use SymfonyComponentValidatorConstraints as Assert;
/**
* User
*
* @ORMTable(name="fos_user")
*@ORMEntity(repositoryClass="AdvertprojectUserBundleEntityUserRepository")            

* / class User extends BaseUser {/ ** * @var integer * * @ORM Column(name =“id”,type =“integer”)* @ORM Id * @ORM GeneratedValue(strategy =“AUTO”) * / protected $ id;

   /**
   * @ORMColumn(type="string", length=255)
   *
   * @AssertNotBlank(message="Please enter the company name.", groups={"Registration", "Profile"})
   * @AssertLength(
   *     min=3,
   *     max=255,
   *     minMessage="The company name is too short.",
   *     maxMessage="The company name is too long.",
   *     groups={"Registration", "Profile"}
   * )
   */
   protected $companyName;

   /**
   * @ORMColumn(type="string", length=255)
   *
   * @AssertNotBlank(message="Please enter your phone.", groups={"Registration", "Profile"})
   * @AssertLength(
   *     min=8,
   *     max=255,
   *     minMessage="The phone value is too short.",
   *     maxMessage="The phone value is too long.",
   *     groups={"Registration", "Profile"}
   * )
   */
   protected $phone;

   /**
   * @ORMColumn(type="string", length=255)
   *
   * @AssertNotBlank(message="Please add details about the person we can contact.", groups={"Registration", "Profile"})
   * @AssertLength(
   *     min=30,
   *     max=255,
   *     minMessage="The details info is too short.",
   *     maxMessage="The details info is too long.",
   *     groups={"Registration", "Profile"}
   * )
   */
   protected $details;

   /**
   * @var
   * @ORMManyToMany(targetEntity="AdvertprojectUserBundleEntityType", cascade={"persist"})
   * @ORMJoinColumn(nullable=false)
   */
   protected $types;

/**
 * @var DateTime
 *
 * @ORMColumn(name="date", type="datetime")
 */
private $date;

/**
 * @ORMColumn(name="updated_at", type="datetime", nullable=true)
 */
private $updatedAt;


public function __construct()
{
    parent::__construct();
    $this->date = new Datetime();
    $this->types = new ArrayCollection();
}

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

/**
 * Set companyName
 *
 * @param string $companyName
 * @return User
 */
public function setCompanyName($companyName)
{
    $this->companyName = $companyName;

    return $this;
}

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

/**
 * Set phone
 *
 * @param string $phone
 * @return User
 */
public function setPhone($phone)
{
    $this->phone = $phone;

    return $this;
}

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

/**
 * Set details
 *
 * @param string $details
 * @return User
 */
public function setDetails($details)
{
    $this->details = $details;

    return $this;
}

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

public function setEmail($email)
{
    $email = is_null($email) ? '' : $email;
    parent::setEmail($email);
    $this->setUsername($email);

    return $this;
}

public function setEmailCanonical($emailCanonical)
{
    $this->emailCanonical = $emailCanonical;
    $this->usernameCanonical = $emailCanonical;
}

/**
 * Get types
 *
 * @return DoctrineCommonCollectionsCollection
 */
public function getTypes()
{
    return $this->types;
}

/**
 * Set date
 *
 * @param DateTime $date
 * @return User
 */
public function setDate($date)
{
    $this->date = $date;

    return $this;
}

/**
 * Get date
 *
 * @return DateTime 
 */
public function getDate()
{
    return $this->date;
}

/**
 * Set updatedAt
 *
 * @param DateTime $updatedAt
 * @return User
 */
public function setUpdatedAt($updatedAt)
{
    $this->updatedAt = $updatedAt;

    return $this;
}

/**
 * Get updatedAt
 *
 * @return DateTime 
 */
public function getUpdatedAt()
{
    return $this->updatedAt;
}

/**
 * Add Type
 *
 * @param AdvertprojectUserBundleEntityType $type
 * @return User
 */
public function addType(Type $type)
{
    $this->types[] = $type;
    return $this;
}

/**
 * Remove types
 *
 * @param AdvertprojectUserBundleEntityType $types
 */
public function removeType(Type $types)
{
    $this->types->removeElement($types);
}
}

Advertproject / UserBundle /表/类型/ RegistrationFormType:

class RegistrationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('companyName', 'text')
        ->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
        ->add('phone', 'text')
        ->add('plainPassword', 'repeated', array(
            'type' => 'password',
            'options' => array('translation_domain' => 'FOSUserBundle'),
            'first_options' => array('label' => 'form.password'),
            'second_options' => array('label' => 'form.password_confirmation'),
            'invalid_message' => 'fos_user.password.mismatch',
        ))
        ->add('details', 'textarea')
        ->add('types', 'entity', array(
            'class' => 'APUserBundle:Type',
            'property' => 'name',
            'required' => true,
            'expanded' => true,
            'multiple' => false,
        ))
    ;
}

Advertproject / UserBundle /实体/类型:

namespace AdvertprojectUserBundleEntity;

use DoctrineORMMapping as ORM;

/**
 * Type
 *
 * @ORMTable(name="type")
 * @ORMEntity(repositoryClass="AdvertprojectUserBundleEntityTypeRepository")
 */
class Type
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORMColumn(name="name", type="string", length=255)
     */
    private $name;


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

    /**
     * Set name
     *
     * @param string $name
     * @return Type
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

我仍然想念你的类型实体。

试试这些东西(一个接一个地检查一下可能会有什么帮助):

  • 接管ManyToMany关系的一个学说示例(在单向或双向之间选择)。 只能将注释更改为@ ORM ...注释。

  • 运行以下命令app/console doctrine:cache:clear-metadata app/console doctrine:cache:clear-result app/console doctrine:cache:clear-query app/console cache-clear app/console cache-clear --env=prod

  • 重新启动(如果可能)您的网络服务器


  • 在你的表单类型中,你正在使用实体类型作为用户的选择包。

    你没有setTypes函数

    1)$ types不是数据库中的字段 - mises @ORM Column(type =“string”,length = 255)(删除多对多关系,添加列注释)

    2)在用户实体中你想存储一个值还是多个?

    如果有的话,就按照我上面提出的那样做。

    如果每个用户可以选择多个项目 - 您的类型实体将用作选项列表,并将数据存储在实体UserChoices中。 然后将UserChoices作为多对一连接到用户实体。

    用于连接实体的类中的属性并不总是数据库中的字段,它们指向存储数据的字段

    链接地址: http://www.djcxy.com/p/69385.html

    上一篇: FOSUserBundle form extension

    下一篇: MappingException while trying to extends from BaseUser in FOSUserBundle