2012-11-22 156 views
1

我用Symfony2開發了一個應用程序,我遇到了問題。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的用戶名,電子郵件,密碼和公司名稱註冊表單。

當用戶填寫字段我想創建新的用戶和propery給了。

我的表單生成器看起來像這樣:

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

你應該創建一個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') 
)); 
+0

感謝您的回答,現在,我有這樣的事情,但我不知道,在Controller中做什麼,如何將用戶添加到數據庫。 –

0

我認爲你可以使用property_path了點。順便說一句,我發現你錯誤的東西與OneToOne關係。如果這是OneToOne爲什麼user.properties是collection/array和User實體有addProperty/removeProperty方法? Here我爲你準備了一些小例子,我將如何解決這個問題(要點)。

相關問題