2013-07-17 25 views
2

我有兩個實體,ViewLocation主義不會持續增加實體關係

每個View可以有一個Location

在我看來,我這樣有:

class View 
{ 
    //..... Other Stuff..... 

    /** 
    * @ManyToOne(targetEntity="Location", inversedBy="views") 
    **/ 
    private $location; 

    //...setters and getters.... 

    public function setLocation($location){ 
     $this->location = $location; 
    } 

} 

,然後我的位置

class Location 
{ 
    //.....other stuff..... 

    /** 
    * @OneToMany(targetEntity="View", mappedBy="location") 
    **/ 
    private $views; 

    public function __construct() { 
     $this->created = $this->updated = new \DateTime("now"); 
     $this->views = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    // .... Getters and Setters .... 
} 

但是當我嘗試這樣做:

<?php 
    $this->pageview = $this->em->getRepository('Entities\View')->find(1); 
    $this->location = $this->em->getRepository('Entities\Location')->find(1); 
    $this->pageview->setLocation($this->location); 
    $this->em->persist($this->pageview); 
    $this->em->flush(); 
?> 

甚至當我創建新實體:

<?php 
    $pv = new Entities\Pageview; 
    $lc = new Entities\Location; 
    $this->em->persist($lc); 
    $this->em->flush(); 
    $pv->setLocation($lc); 
    $this->em->persist($pv); 
    $this->em->flush(); 
?> 

Doctrine 從來沒有設置數據庫中的location_id(它始終爲NULL)。

我檢查的SQL查詢和他們還沒有被嘗試在被設置,所有我得到的是:

INSERT INTO View (field1, field2, created, updated) VALUES ('val1', 'val2', '2013-07-17T12:10:56+01:00', '2013-07-17T12:10:56+01:00') 

沒有提到任何地方......奇怪的是我可以更新FIELD1和FIELD2罰款......和所有其他關係在我的應用程序的工作......我只是無法得到的觀點和地點工作...

編輯

我有確切一些代碼正在工作另一臺電腦。我不知道爲什麼它不工作,但我只是將文件移回並重新啓動我的電腦,現在它是...緩存問題,我猜?

回答

1

重新啓動我的電腦和問題得到有效解決......我不不知道爲什麼它出錯了!

也許與緩存或代理有關...我不知道...

+0

查看http://stackoverflow.com/a/40526019/1794894瞭解更多詳情 – Rvanlaak

0

您可以嘗試顯式引用Doctrine需要進行連接的正確列。

/** 
* @ManyToOne(targetEntity="Location") 
* @JoinColumn(name="location_id", referencedColumnName="id") 
*/ 
private $location; 

而且,在這個例子中:

$this->pageview = $this->em->getRepository('Entities\View')->find(1); 
$this->location = $this->em->getRepository('Entities\Location')->find(1); 
$this->pageview->setLocation($this->location); 
$this->em->persist($this->pageview); 
$this->em->flush(); 

你並不需要堅持的實體,如果你只是更新現有的數據。

+0

仍然沒有運氣我害怕...雖然有點更新,當我嘗試使用DQL做到這一點時:'錯誤:類實體\視圖沒有字段或關聯命名位置! 「位置」可能是一個關鍵字!? –

+0

從頭開始,我嘗試過使用不同的名字......沒有運氣......它只是沒有創建關聯! –

+0

當你在(例如getRepository('Entities \ View'))中指定你的實體時,就是完整的命名空間路徑,因爲據我所知,Doctrine只會使用完全限定的命名空間。 –

0

我認爲你需要在該位置加載視圖。所以,你必須在你的位置的實體創建一個方法是這樣的:

public function getViews() { 
    return $this->views; 
} 

,然後持續到數據庫中,這樣做:

$location = new Entity\Location(); 
$view = new Entity\View(); 
$location->getViews()->add($view); 
$this->em->persist($location) 
$view->setLocation($location); 
$this->em->persist($view); 
$this->em->flush(); 
0

這與學說ORM緩存驅動程序:

doctrine: 
    orm: 
     entity_managers: 
      default: 
       metadata_cache_driver: apcu 
       result_cache_driver: apcu 
       query_cache_driver: apcu 

我們使用APCu甚至上DEV做緩存,清除APCu(由重啓動Apache)的伎倆。