1

我用 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')
    ));
}

我想创建像文本一样的字段“公司”,而不是选择或单选按钮。我可以这样做吗,如果可以,怎么做?

4

3 回答 3

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')
));
于 2012-11-27T15:00:36.987 回答
0

我认为您可以为此使用property_path。顺便说一句,我看到您在 OneToOne 关系中犯了一些错误。如果这是 OneToOne 为什么 user.properties 是集合/数组,而用户实体有 addProperty/removeProperty 方法?在这里,我为您准备了一些小例子,我将如何解决这个问题(要点)。

于 2012-12-01T12:42:30.830 回答
0

只需查看 Data Transformers http://symfony.com/doc/2.0/cookbook/form/data_transformers.html在此场景中,他们通过字符串(在您的情况下为公司名称)接收内容,但当然不是抛出错误您也可以创建一个实体。

于 2012-11-26T11:41:46.897 回答