2012-08-08 88 views
15

我有一個產品表和另一個表與Notes。每個產品可以有或沒有一些筆記。我只需要筆記就可以知道他們的產品,但產品不知道它的筆記。我認爲,這應該是我的代碼:使用語句@OneToOne - Doctrine2

namespace EM\MyBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

use EM\MyBundle\Entity\Productss; 

/** 
* @ORM\Entity 
* @ORM\Table(name="notes") 
*/ 
class Notess 
{ 
/** 
* @ORM\Id 
* @ORM\Column(type="integer") 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** 
* @OneToOne(targetEntity="Productss") 
* @JoinColumn(name="products_id", referencedColumnName="id") 
**/ 
private $products; 
//... 
} 

namespace EM\MyBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 

/** 
* @ORM\Entity 
* @ORM\Table(name="domains") 
*/ 
class Domains 
{ 
/** 
* @ORM\Id 
* @ORM\Column(type="integer") 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 
// ... 
} 

,但它給出了一個錯誤:[主義\ COMMON \註解\ AnnotationException]

[語義錯誤]物業EM註釋 「@OneToOne」 \ MyBundle \ Entity \ Notes :: $產品從未導入。你有沒有忘記爲這個註釋添加「使用 」聲明?

你能幫我解決這個問題嗎?

回答

51

我覺得你註定要使用@ORM\OneToOne,而不是...

/** 
* @ORM\OneToOne(targetEntity="Productss") 
* @ORM\JoinColumn(name="products_id", referencedColumnName="id") 
*/ 
private $products; 
//... 
} 
+0

非常感謝您!它正在工作! :) – Faery 2012-08-08 07:32:14

+0

由於文檔的原因,出現這種問題的原因是:OneToMany,oneToOne或manyToMany。我有同樣的問題只是複製/粘貼官方文檔:http://docs.doctrine-project.org/en/latest/reference/association-mapping.html – 2013-02-10 20:23:48

+2

這將是因爲教條文檔假設你會使用每個'@'類,所以所有註釋都使用實際的類名('使用Doctrine/ORM/Mapping/OneToOne' =>'@ OneToOne'),Symfony建議使用Doctrine \ ORM \ Mapping AS ORM'命名空間方法,所有註釋都使用ORM作爲基礎('@ORM \ OneToOne')。 – qooplmao 2014-02-16 01:36:05