2012-10-12 44 views
4

我有一個Symfony2項目插入數據庫。對於每個表我都有一個實體。Symfony2:數據庫和實體設置:既沒有屬性...也沒有方法...也沒有方法...存在於類

現在,我試圖用ManyToOne連接一個實體與另一個實體。

這裏是問題:

我有兩個實體:用戶和工作場所。

在用戶實體,我有:

/** 
* @ORM\ManyToOne(targetEntity="Workplace") 
* @ORM\JoinColumn(name="workplace", referencedColumnName="place") 
**/ 
protected $workplace; 

/** 
* Set workplace 
* 
* @param integer $workplace 
*/ 
public function setWorkplace($workplace) 
{ 
    $this->workplace = $workplace; 
} 

/** 
* Get workplace 
* 
* @return integer 
*/ 
public function getWorkplace() 
{ 
    return $this->workplace; 
} 

在工作場所的實體,我有:

/** 
* @ORM\Column(type="text") 
*/ 
protected $place; 



/** 
* Set place 
* 
* @param text $place 
*/ 
public function setPlace($place) 
{ 
    $this->place = $place; 
} 

/** 
* Get place 
* 
* @return text 
*/ 
public function getPlace() 
{ 
    return $this->place; 
} 

有了這樣的,我得到一個異常:

Neither property "workplace" nor method "getWorkplace()" nor method "isWorkplace()" exists in class "SciForum\Version2Bundle\Entity\Workplace" 

這怎麼能解決。非常感謝你。

+0

我想你已經在每個實體錯過setter和getter函數。 –

+0

通常情況下,我會用更多的細節編輯我的問題。 –

+0

你可以發送你的控制器代碼和工作場所實體嗎? –

回答

6

在表單類型試試這個

->add('place','entity', array('class'=>'yourBundle:WorkPlace', 
           'property'=>'place')) 

+1

非常感謝。 –

5

@Asish美聯社是對的,但缺少解釋。

在formBuilder中,如果你有兩個實體之間的關係,你必須在你的表單類型中指定正確的實體。

->add(
    'place', 
    'entity', 
     array(
      'class'=>'yourBundle:WorkPlace', //Link your entity 
      'property'=>'place' //Specify the property name in the entity 
)) 

如果在formBuilder,是不存在的屬性來指定,你都會有這樣的錯誤:

Neither property "workplace" nor method "getWorkplace()" nor method "isWorkplace()" exists in class "SciForum\Version2Bundle\Entity\Workplace" 

那是你錯誤的原因和解決方案的說明。

https://creativcoders.wordpress.com/2014/06/02/sf2-neither-the-property-nor-one-of-the-methods-exist-and-have-public-access-in-class/

相關問題