簡體   English   中英

Symfony2實體字段

[英]Symfony2 entity field

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

我想創建字段“公司”,例如文本,而不是選擇或單選按鈕。 如果可以,我可以怎么做?

只需在此方案中查看Data Transformers http://symfony.com/doc/2.0/cookbook/form/data_transformers.html ,他們就會通過字符串(在您的情況下為公司名稱)接收內容,但是當然不會拋出錯誤您也可以創建一個實體。

您是否應該創建自定義表單字段類型

如前:

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

我認為您可以為此使用property_path 順便說一句,我看到您對OneToOne關系有誤。 如果這是OneToOne,為什么user.properties是collection / array,並且User實體具有addProperty / removeProperty方法? 在這里,我為您准備了一個小例子,說明如何解決(要點)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM