简体   繁体   English

教义不会添加持久实体关系

[英]Doctrine won't add persist entity relationship

I have two entities, View and Location 我有两个实体, ViewLocation

Each View can have a Location . 每个View可以有一个Location

In my view I thus have: 因此,我认为:

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

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

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

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

}

and then for my 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 ....
}

But when I try and do this: 但是当我尝试这样做时:

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

Or even when I create new entities: 甚至当我创建新实体时:

<?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 never sets the location_id in the database (it is always NULL). 原则永远不会在数据库中设置location_id(始终为NULL)。

I've checked the SQL queries and they're not even being attempted at being set, all I'm getting is: 我已经检查了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')

No reference to locations whatsoever...The weird thing is I can update field1 and field2 fine...and all other relations are working throughout my application...I just can't get views and locations to work... 根本没有引用位置...奇怪的是我可以很好地更新field1和field2 ...并且所有其他关系都在我的整个应用程序中正常工作...我只是无法获取视图和位置...

EDIT 编辑

I have the exact some code working now on another computer. 我现在有确切的代码可以在另一台计算机上工作。 I don't know why it wasn't working, but I just moved the files back and restarted my computer and now it is...cacheing problem I guess? 我不知道为什么它不起作用,但我只是将文件移回并重新启动计算机,现在是...我猜是缓存问题?

Restarted my computer and the problem got solved...I don't know why it was going wrong! 重新启动计算机,问题解决了...我不知道为什么出问题了!

Maybe something to do with caches or proxies...I dunno... 也许与缓存或代理有关...我不知道...

You could try explicitly referencing the correct columns that Doctrine needs to do a join on. 您可以尝试显式引用Doctrine进行连接所需的正确列。

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

Also, in this example: 另外,在此示例中:

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

You do not need to persist the entity if you are just updating the existing data. 如果您仅更新现有数据,则无需保留实体。

I think you need load the view in the location. 我认为您需要在该位置加载视图。 So you must create a method in your Location entity like this: 因此,您必须像这样在Location实体中创建一个方法:

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

and then to persist into database, do this: 然后坚持到数据库中,执行以下操作:

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

This is related to the Doctrine ORM cache drivers: 这与Doctrine ORM缓存驱动程序有关:

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

We used APCu to even on DEV do caching, clearing APCu (by restarting Apache) did the trick. 我们甚至使用APCu甚至在DEV进行缓存,清除APCu(通过重新启动Apache)也可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM