FOSUserBundle表单扩展名

时间:2015-10-01 14:15:14

标签: symfony fosuserbundle

我成功覆盖了FOSUserbundle的User类,添加了一堆属性。但当我尝试添加'types'属性时,您可以看到下面的内容,我有以下异常:

  

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

Type是一个单独的实体,与我的托管实体/用户具有ManyToMany关系。

我们的想法是在主窗体中插入单选按钮表单。我已经阅读了很多这里提供的类似错误消息的答案,但没有任何效果。实际上我的情况类似于this(由@Mick提供答案)。但是提供的建议在我的案例中不起作用,因为我完全按照那里的建议行事。任何人都可以帮我解决这个问题吗?请注意,使用单选按钮表单正确呈现表单,并在按下提交按钮后返回异常。

Advertproject / UserBundle /实体/用户:

namespace Advertproject\UserBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
/**
* User
*
* @ORM\Table(name="fos_user")
*@ORM\Entity(repositoryClass="Advertproject\UserBundle\Entity\UserRepository")            

* /    class用户扩展BaseUser    {         / **         * @var整数         *         * @ORM \ Column(name =“id”,type =“integer”)         * @ORM \ Id        * @ORM \ GeneratedValue(strategy =“AUTO”)        * /        protected $ id;

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

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

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

   /**
   * @var
   * @ORM\ManyToMany(targetEntity="Advertproject\UserBundle\Entity\Type", cascade={"persist"})
   * @ORM\JoinColumn(nullable=false)
   */
   protected $types;

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

/**
 * @ORM\Column(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 \Doctrine\Common\Collections\Collection
 */
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 \Advertproject\UserBundle\Entity\Type $type
 * @return User
 */
public function addType(Type $type)
{
    $this->types[] = $type;
    return $this;
}

/**
 * Remove types
 *
 * @param \Advertproject\UserBundle\Entity\Type $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 Advertproject\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Type
 *
 * @ORM\Table(name="type")
 * @ORM\Entity(repositoryClass="Advertproject\UserBundle\Entity\TypeRepository")
 */
class Type
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(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;
    }
}

2 个答案:

答案 0 :(得分:0)

我仍然缺少你的Type实体。

尝试这些事情(逐个检查,然后检查一下可能会有什么帮助):

  • 接管doctrine's examples中的一个用于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

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

答案 1 :(得分:0)

在您的表单类型中,您使用实体类型作为用户选项包。

你没有setTypes函数

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

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

如果有的话,就像我上面提出的那样。

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

为实体之间的连接而做的clases中的

属性并不总是数据库中的字段,它们指向存储数据的字段