2013-10-17 40 views
0

實體名稱爲CountryRegion我有兩個實體和兩個存儲庫。如何將數據保存在實體中?

存儲庫命名爲CountryRepositoryRegionRepository

如何將數據存儲在Region實體中?

$em = $this->container->get('doctrine')->getEntityManager(); 

$countryId=$em->getRepository('LocationBundle:Country')->find(1)); 
$region=new Region(); //How to create Region Ojbect 
$region->setCountryId($countryId); 
$region->setName('abc'); 
$region->save(); 
+0

閱讀這裏的文檔,更好... http://docs.doctrine-project.org/ EN/2.0.x版本/參考/工作與 - objects.html#持續存在的實體 – pbenard

回答

0

首先,你應該修改你控制器操作的第一行的是這樣:

$em = $this->getDoctrine()->getManager(); 

其次,你必須實現在實體區域的構造,並在控制器,你必須正確傳遞構造函數的參數。

最後,保存新的地區進入數據庫,喲必須使用以下語句:

$em->persist($region); 
    $em->flush();