学说2 - 以多对一的关系持久化实体

时间:2022-10-22 18:46:15

I've been struggling with this for some hours now and have read through the Doctrine documentation and more documentation but I'm still struggling.

我一直在努力解决这个问题几个小时,并阅读了Doctrine文档和更多文档,但我仍在苦苦挣扎。

I have two tables. My "lookups" table is the owner (many) in the relationship with the "lookup_statuses" table (one). There are multiple different statuses that a particular lookup can be in.

我有两张桌子。我的“查找”表是与“lookup_statuses”表(一个)的关系中的所有者(很多)。特定查找可以有多种不同的状态。

This is (I think) a uni-directional one-to-many relationship.

这是(我认为)单向一对多的关系。

In code I search the lookup_statuses table and get back an object that matches the slug I need. I call setLookupStatus on my lookup entity and try to persist it. I get the error 'A new entity was found through the relationship'.

在代码中,我搜索lookup_statuses表并返回一个与我需要的slug匹配的对象。我在查找实体上调用setLookupStatus并尝试保留它。我收到错误'通过关系找到了一个新实体'。

I used the doctrine reverse engineering to start off my domain model but I just cannot work out how to properly implement this relationship.

我使用了doctrine逆向工程来开始我的域模型,但我无法弄清楚如何正确实现这种关系。

My LookupStatuses class (one) looks like this:

我的LookupStatuses类(一个)看起来像这样:

namespace App\Lib\Domain\Datalayer;

/**
 * LookupStatuses
 *
 * @ORM\Table(name="lookup_statuses")
 * @ORM\Entity
 */
class LookupStatuses
{
    /**
     * @OneToMany(targetEntity="App\Lib\Domain\Datalayer\Lookups", mappedBy="lookupStatus", cascade={"persist", "remove"})
     */
    private $lookups;
}

And my Lookups table (many) looks like this:

我的Lookups表(很多)看起来像这样:

namespace App\Lib\Domain\Datalayer;

/**
 * Lookups
 *
 * @ORM\Table(name="lookups", indexes={@ORM\Index(name="IDX_4CEC819D037A087", columns={"lookup_status_id"}), @ORM\Index(name="IDX_4CEC8194D39DE23", columns={"camera_event_id"})})
 * @ORM\Entity
 */
class Lookups
{
   /**
     * @var \App\Lib\Domain\Datalayer\LookupStatuses
     *
     * @ORM\ManyToOne(targetEntity="App\Lib\Domain\Datalayer\LookupStatuses", inversedBy="lookups", cascade={"persist"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="lookup_status_id", referencedColumnName="id")
     * })
     */
    private $lookupStatus;
}

Here are the versions of the stack I'm using:

这是我正在使用的堆栈的版本:

"php": ">=5.4.0",
"laravel/framework": "5.0.*@dev",
"doctrine/orm": ">=2.4.0",

Please can somebody help me unravel this so that my use case will work. I want to:

请有人帮我解开这个,这样我的用例就可以了。我想要:

  1. Look up a status in the lookup_statuses code
  2. 在lookup_statuses代码中查找状态

  3. Call setLookupStatus() on my lookup entity with the object I get
  4. 使用我得到的对象在我的查找实体上调用setLookupStatus()

  5. Persist the lookup to save the new status code
  6. 保留查找以保存新的状态代码

---- update : including code ----

----更新:包括代码----

This is sample code:

这是示例代码:

$lookupStatusesService = ServiceFactory::getInstance('LookupStatuses');

$status = $lookupStatusesService->findOne(['slug' => $slug], true);

$cameraEventService = ServiceFactory::getInstance('CameraEvents');

$cameraEvent = $cameraEventService->findOne(['uuid' => $uuid], true);

$unicodeLookupService = ServiceFactory::getInstance('UnicodeLookups');

$unicodeLookup = $unicodeLookupService->findOne(['cameraEvent' => 

$cameraEvent->getId()], true);

$unicodeLookup->setLookupStatus($status);

EntityManager::persist($unicodeLookup);

EntityManager::flush();

Findone() builds up querybuilder where clauses and returns

Findone()构建了querybuilder where子句并返回

$queryBuilder->getQuery()->getResult();

1 个解决方案

#1


After struggling for 3 hours with this I solved it on the walk to my car from the office.

经过3个小时的努力,我从办公室走到我的车上解决了这个问题。

The problem is that I was using Memcached.

问题是我使用的是Memcached。

The object I was getting from my findby was a Doctrine Entity, but was returned from cache instead of coming from Doctrine. That's why Doctrine thought it was new. If I insist on getting a fresh result from the database the code works.

我从findby获得的对象是一个Doctrine Entity,但是从缓存返回而不是来自Doctrine。这就是为什么Doctrine认为它是新的。如果我坚持从数据库中获取新的结果,代码就可以工作。

I suspect that because I'm using a unidirectional relationship the @OneToMany in LookupStatuses is superfluous.

我怀疑因为我使用的是单向关系,所以LookupStatuses中的@OneToMany是多余的。

#1


After struggling for 3 hours with this I solved it on the walk to my car from the office.

经过3个小时的努力,我从办公室走到我的车上解决了这个问题。

The problem is that I was using Memcached.

问题是我使用的是Memcached。

The object I was getting from my findby was a Doctrine Entity, but was returned from cache instead of coming from Doctrine. That's why Doctrine thought it was new. If I insist on getting a fresh result from the database the code works.

我从findby获得的对象是一个Doctrine Entity,但是从缓存返回而不是来自Doctrine。这就是为什么Doctrine认为它是新的。如果我坚持从数据库中获取新的结果,代码就可以工作。

I suspect that because I'm using a unidirectional relationship the @OneToMany in LookupStatuses is superfluous.

我怀疑因为我使用的是单向关系,所以LookupStatuses中的@OneToMany是多余的。