【问题标题】:Symfony2 entity fieldSymfony2实体字段
【发布时间】:2012-11-10 23:56:56
【问题描述】:

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

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

【问题讨论】:

    标签: php doctrine-orm entity symfony-2.1


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      你应该创建一个Custom Form Field Type

      例如:

      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')
      ));
      

      【讨论】:

      • 感谢您的回答,现在,我有这样的东西,但我不知道在Controller中做什么,如何将用户添加到数据库。
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2015-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-07
        相关资源
        最近更新 更多