【问题标题】:Doctrine won't add persist entity relationship教义不会添加持久实体关系
【发布时间】:2013-07-17 11:13:26
【问题描述】:

我有两个实体,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...所有其他关系在我的整个应用程序中都有效...我只是无法获得视图和位置工作.. .

编辑

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

【问题讨论】:

    标签: php doctrine-orm doctrine associations model-associations


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 恐怕还是没有运气......不过有一点更新,当我尝试使用 DQL 进行操作时,我得到:Error: Class Entities\View has no field or association named locationlocation 可能是关键字吗!?
      • 从头开始,我尝试使用不同的名称...没有运气...只是没有创建关联!
      • 当你在 (eg getRepository('Entities\View')) 中指定实体时,就是完整的命名空间路径,因为据我所知,Doctrine 只会使用完全限定的命名空间。跨度>
      • '这不是命名空间问题...嗯...我把代码带回家到我的另一台计算机上,由于某种原因它在那里工作正常...我绝对有 知道为什么...可能是缓存了错误的内容或使用了一些错误的代理信息?
      【解决方案3】:

      我认为您需要在该位置加载视图。所以你必须在你的 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();
      

      【讨论】:

        【解决方案4】:

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

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

        我们使用 APCu 甚至在 DEV 上进行缓存,清除 APCu(通过重新启动 Apache)就可以了。

        【讨论】:

          猜你喜欢
          • 2015-07-27
          • 2020-06-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多