Alice fixtures - Persist and reference a first set of entities/objects

空扰寡人 提交于 2019-12-11 06:02:59

问题


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

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:

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

My 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 ↓

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'.

Thank you.


回答1:


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

When loading a set of fixtures you can inject parameters & objects. So if you load 2 files, you can persist it and then load the remaining files passing the results of the previous load to it.




回答2:


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.

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!#

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



来源:https://stackoverflow.com/questions/49648923/alice-fixtures-persist-and-reference-a-first-set-of-entities-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!