2012-09-01 53 views
3

考慮以下問題:
我有一個多到一個協會和Author之間Article學說關聯映射和ArrayCollection的堅持錯誤

class Author{ 
    /** 
    * ... 
    *@var ArrayCollection_of_Article 
    */ 
    public $articles; 
} 

class Article{ 
    /** 
    * ... 
    *@var Author 
    */ 
    public $author; 
} 

製作一個新Article我有兩種類型的代碼:
第一之一:

$author = ORM::Find("Author",12); // fetch an Author with ID=12 
$art = new Article(); 
$art->author=$author; 
$author->articles->add($art); 
ORM::Persist($art); // persist it to write to database 

第二之一:(省略第4行)

$author = ORM::Find("Author",12); // fetch an Author with ID=12 
$art = new Article(); 
$art->author=$author; 
ORM::Persist($art); // persist it to write to database 

哪一個是正確的?
第一個工作正常。但是,第二個導致錯誤,例如下面的錯誤有時:

A new entity was found through a relationship that was not configured to cascade persist operations 

我想知道第二個是可能的還是它會總是原因SQL錯誤。
謝謝...

+0

第二次導致任何錯誤?哪一個以防萬一? – moonwave99

+0

第二個導致錯誤**有時** –

+0

根據天氣情況?請發佈整個orm註釋/配置文件,並附帶消息錯誤[是PDO/Mysql錯誤還是PHP錯誤btw?]。 – moonwave99

回答

0

你必須使用getters和setter來訪問關係,這是因爲Doctrine構建了一個Proxy類,它從你的Entity類中延伸出來並覆蓋你的getters來提供延遲加載。