繁体   English   中英

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

[英]Doctrine won't add persist entity relationship

我有两个实体, 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();
?>

原则永远不会在数据库中设置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 ...并且所有其他关系都在我的整个应用程序中正常工作...我只是无法获取视图和位置...

编辑

我现在有确切的代码可以在另一台计算机上工作。 我不知道为什么它不起作用,但我只是将文件移回并重新启动计算机,现在是...我猜是缓存问题?

重新启动计算机,问题解决了...我不知道为什么出问题了!

也许与缓存或代理有关...我不知道...

您可以尝试显式引用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();

如果您仅更新现有数据,则无需保留实体。

我认为您需要在该位置加载视图。 因此,您必须像这样在Location实体中创建一个方法:

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

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

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

我们甚至使用APCu甚至在DEV进行缓存,清除APCu(通过重新启动Apache)也可以解决问题。

暂无
暂无

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

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