Php 赢得';t添加持久实体关系

Php 赢得';t添加持久实体关系,php,doctrine-orm,doctrine,associations,model-associations,Php,Doctrine Orm,Doctrine,Associations,Model Associations,我有两个实体,视图和位置 每个视图都可以有一个位置 因此,我认为: class View { //..... Other Stuff..... /** * @ManyToOne(targetEntity="Location", inversedBy="views") **/ private $location; //...setters and getters.... public function setLocation($lo

我有两个实体,
视图
位置

每个
视图
都可以有一个
位置

因此,我认为:

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();
?>
没有提到任何位置…奇怪的是我可以很好地更新field1和field2…并且所有其他关系在我的应用程序中都起作用…我只是无法获得视图和位置

编辑


我现在在另一台计算机上运行着一些精确的代码。我不知道为什么它不起作用,但我只是将文件移回并重新启动了我的计算机,现在它是…缓存问题,我想?

您可以尝试显式引用Dority需要在其上执行联接的正确列

/**
 * @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();

如果只是更新现有数据,则不需要持久化实体。

我认为您需要在该位置加载视图。因此,您必须在位置实体中创建如下方法:

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();

重新启动我的电脑,问题解决了…我不知道为什么会出问题


可能与缓存或代理有关…我不知道…

这与ORM缓存驱动程序有关:

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

我们使用
APCu
甚至在
DEV
上进行缓存,清除APCu(通过重新启动Apache)完成了这项任务。

恐怕还是没有运气。。。不过有一点更新,当我尝试使用DQL时,我得到:
错误:Class Entities\View没有名为location的字段或关联
location
可能是关键词吧!?请注意,我尝试过使用不同的名称…运气不好…只是没有创建关联!!当您在(例如getRepository('entities\View')中指定实体时,是指完整的名称空间路径,因为据我所知,条令将只使用完全限定的名称空间。“这不是名称空间问题……嗯……我将代码带回了我的另一台计算机,出于某种原因,它在那里工作得很好…我完全不知道为什么…也许它缓存了一些错误的东西或使用了一些不好的代理信息?
$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();
doctrine:
    orm:
        entity_managers:
            default:
                metadata_cache_driver: apcu
                result_cache_driver: apcu
                query_cache_driver: apcu