爱丽丝装置-坚持并引用第一组实体/对象 [英] Alice fixtures - Persist and reference a first set of entities/objects

查看:111
本文介绍了爱丽丝装置-坚持并引用第一组实体/对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

爱丽丝(Alice)在3.x版本中放弃了持久层.

Alice has given away the persistence layer in 3.x.

在尝试从2.2迁移到3.0.x时,…
我需要先加载并保存一些灯具(以便填充它们的id),然后再从另一堆灯具文件中引用这些实体ID.
如何实现?

In the attempt of migrating from 2.2 to 3.0.x, …
I need to load and persist some fixtures first (so their id get populated) and then reference those entities ids from another bunch of fixture files.
How can this be achieved ?

我想我可能必须分别循环并加载多个夹具文件集,但是我不知道在这种情况下对象引用将如何生存.

I guess I may have to loop and load multiple fixture files sets separately but I have no idea how objects references will subsist in such scenario.

我的设置当前不起作用,但提示以下错误:

My setup currently doesn't work but causes the following error to prompt:

为对象"bar1"的属性"fooDbId"提供了无效的值 (类:Doctrine \ Model \ Bar).

Invalid value given for the property "fooDbId" of the object "bar1" (class: Doctrine\Model\Bar).

我的 FixturesLoader.php :

// …
use Nelmio\Alice\Loader\NativeLoader as AliceLoader;
// …
$loader = new AliceLoader();
$entities = $loader
    ->loadFiles(
        [
            __DIR__.'/foo.yml',
            /* ↓ Some more fixture files ↓ */,

           # Here comes "bar" which references persisted foo entities id
           # through its "fooDbId" property using expresion '@foo1->id'
            __DIR__.'/bar.yml'
        ],
        ['locale' => 'en_EN']
    )
    ->getObjects();

foreach ($entitites as $entity) {
    $manager->persist($entity)
}

$manager->flush();

bar.yml :

Doctrine\Model\Bar:
    bar1:
        fooDbId: '@foo1->id'
        # ↓ More properties ↓


编辑

fooDbId不是真实"关系/外键字段,而是0左侧填充的Foo实体ID. (Bar :: setFooDbId负责左填充操作).例如:给定一个Foo实例,其 id 属性值为87,则关联的Bar实例的fooDbId属性应等于'00000087'.


Edit

fooDbId is not a "real" relation/foreign-key field but the 0 left-padded Foo entity id. (Bar::setFooDbId is in charge of the left-padding operation). Ex: given a Foo instance with an id property value of 87, the associated Bar instance should have its fooDbId property equal to '00000087'.

谢谢.

推荐答案

首先,没有必要为每个实体创建单独的"yml"文件.

First, it's not necessary to create separate 'yml' file for each entity.

第二,据我了解,您正在尝试在学说中伪造您的多对一关系.如果是这样,则不需要这种方法.原则总是帮助您考虑对象而不是数据库.这是一个例子,爱丽丝将如何帮忙.

Secondly, As I understood you are trying to fake your Many to One relationship in doctrine. If so, you don't need this approach. Doctrine always help you to think about objects instead of database. Here is an example how Alice will do the favor.

想象一下,酒吧实体与Foo有多对一关系.这是示例:

Imagine that Bar entity has Many To One relation with Foo. Here is the example:

App\Entity\Foo:
    foo_{1..10}:
        title: <text(20)>
        isFeatured: <boolean(50)>

App\Entity\Bar:
    bar_{1..10}:
        username: <userName()>
        foo: '@foo_1' # This will assign the first foo generated above#

        foo: '@foo_*' # This will assign one the 10 generated foos above randomly!#

同样,Doctrine将像往常一样为您添加外键.

Again, Doctrine will add the foreign key for you as always.

这篇关于爱丽丝装置-坚持并引用第一组实体/对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆