我用 Symfony2 开发了一个应用程序,但我遇到了问题。
我有用户实体类,在这个类中我有:
/**
* @ORM\OneToOne(targetEntity="UserProperty", mappedBy="user")
*/
protected $properties;
和
/**
* Add properties
*
* @param Flashwand\UserBundle\Entity\UserProperty $property
* @return User
*/
public function addProperties(\Warski\Flashwand\UserBundle\Entity\UserProperty $property)
{
$this->properties[] = $property;
return $this;
}
/**
* Remove properties
*
* @param Flashwand\UserBundle\Entity\UserProperty $propertie
*/
public function removeProperties(\Warski\Flashwand\UserBundle\Entity\UserProperty $property)
{
$this->properties->removeElement($property);
}
/**
* Get properties
*
* @return Doctrine\Common\Collections\Collection
*/
public function getProperties()
{
return $this->properties;
}
在 UserProperty 类中,我有:
/**
* @ORM\Id
* @ORM\OneToOne(targetEntity="User", inversedBy="properties")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* @ORM\Column(name="phone", type="string", length=55, nullable=true)
*/
protected $phone = null;
/**
* @ORM\Column(name="fax", type="string", length=55, nullable=true)
*/
protected $fax = null;
/**
* @ORM\Column(name="company", type="string", length=150, nullable=true)
*/
protected $company = null;
/**
* @ORM\Column(name="job_description", type="string", length=255, nullable=true)
*/
protected $job_description = null;
现在,我正在尝试使用来自 UserProperty 的用户名、电子邮件、密码和公司名称来构建注册表单。
当用户填写字段时,我想创建新的用户和属性。
我的表单生成器如下所示:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('email', 'email', array(
'label' => 'Email',
'label_attr' => array('class' => 'control-label')
));
$builder->add('password', 'repeated', array(
'type' => 'password',
'invalid_message' => 'Password don\'t match.',
'first_options' => array(
'label' => 'Password',
'label_attr' => array('class' => 'control-label')
),
'second_options' => array(
'label' => 'Retype password',
'label_attr' => array('class' => 'control-label')
),
));
$builder->add('firstname', 'text', array(
'label' => 'Firstname',
'label_attr' => array('class' => 'control-label')
));
$builder->add('lastname', 'text', array(
'label' => 'Lastname',
'label_attr' => array('class' => 'control-label')
));
$builder->add('email', 'email', array(
'label' => 'Email',
'label_attr' => array('class' => 'control-label')
));
$builder->add('properties', 'entity', array(
'class' => 'FlashwandUserBundle:UserProperty',
'property' => 'company',
'multiple' => false,
'expanded' => false,
'label' => 'Company',
'label_attr' => array('class' => 'control-label')
));
}
我想创建像文本一样的字段“公司”,而不是选择或单选按钮。我可以这样做吗,如果可以,怎么做?