我用Symfony2开发了一个应用程序,我遇到了问题。
我有User Entity类,在这个类中我有:
/**
* @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')
));
}
我想创建字段“公司”,如文本,而不是选择或单选按钮。 我能这样做,如果有,怎么样?
答案 0 :(得分:0)
只需查看数据转换器http://symfony.com/doc/2.0/cookbook/form/data_transformers.html,在此场景中,他们通过字符串(在您的情况下是公司名称)接收内容,但当然不是抛出错误,您也可以创建实体。
答案 1 :(得分:0)
就像前:
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class PropertiesType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('company')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Flashwand\UserBundle\Entity\UserProperty',
));
}
public function getName()
{
return 'properties';
}
}
更改表单构建器
$builder->add('properties', new PropertiesType(), array(
'label' => 'Company',
'label_attr' => array('class' => 'control-label')
));
答案 2 :(得分:0)
我认为您可以使用property_path。顺便说一句,我看到你误解了OneToOne关系。如果这是OneToOne,为什么user.properties是集合/数组,而User实体有addProperty / removeProperty方法? Here我为你准备了一些小例子,我将如何解决这个问题(要点)。