原则:坚持某些实体不会导致数据库发生变化



我有一个fixture类,它扩展了Doctrine\Bundle\FixturesBundle\fixture,我正在读取一堆csv表,其中的数据是硬编码的。

它运行得很好,我引入了越来越多的实体类(User、Location等(,直到其中一个文件(Topic.csv(persist($entity)突然没有效果。装载夹具离开工作台";主题";仍然完全空着。没有错误消息,日志中没有显示任何内容。

foreach ($files as $file) {

// Here some magic happens that converts each line into an associative array 
// e.g. [['id' => 1, 'Name' => 'Bob', 'Age' => 21, ...], ['id' => 2, ...]]

foreach ($rows as $row) {
$create_method = 'create' . $shortClassName;   // e.g. createUser
$entity = $this->$create_method($row);
$this->om->persist($entity);
// This part below is to ignore @ORMGeneratedValue and use the given id values
// instead
// $this->om has the class DoctrinePersistenceObjectManager

$metadata = $this->om->getClassMetaData(get_class($entity));  
$metadata->setIdGeneratorType(DoctrineORMMappingClassMetadata::GENERATOR_TYPE_NONE);
}
$this->om->flush();
}

使用我的调试器,我可以看到实体已经创建,看起来很好,并交给了对象管理器,但在持久化操作之后,我在UnitOfWork中找不到任何实体,也找不到持久化的任何痕迹。就好像Topic实体被无声地丢弃了一样。

有人知道我如何继续调试吗?我应该把断点放在哪里,看看为什么这些实体永远不会出现在我的数据库中

我正在运行Symfony 5.1,我的原则/坚持目前是1.3.7。

所以我找到了答案:在我努力不给我的实体增加复杂性的过程中,我觉得没有必要添加关联的反面。

所以我的位置看起来是这样的:

class Location
{ 
/** @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue
*/
protected int $id;
/** @ORMColumn(type="string") */
protected string $Name;
// [...] other fields and getters and setters omitted
}

我的主题是这样的:

class Topic
{ 
/** @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue
*/
protected int $id;
/** @ORMColumn(type="string") */
protected string $Name;
/** @ORMManyToOne(targetEntity="Location", inversedBy="Topics")     * */
protected Location $Location;
// [...] other fields and getters and setters omitted
}

没有包括在Location中的是

/** @ORMOneToMany(targetEntity="Topic", mappedBy="Location") */
protected Collection $Topics;

我认为这是可选的,以防你想使用教条的奇妙自动魔法为你获取所有对象。

但不,这不是可选的。在没有指定OneToMany关系的情况下,没有任何内容被持久化。

我想这次我需要重读一遍,试着真正理解它。

有人知道为什么没有抛出错误,以及为什么我的实体被忽略了吗?

最新更新