使用Hautleook / AliceBundle引用Bundles之间的夹具

时间:2016-03-12 00:19:55

标签: php symfony fixtures nelmio-alice

任何人都可以使用Simfony 2,7中的HautellokAliceBundle为我提供一个关于在两个分离包之间引用datafixture yml文件的示例。我需要在FOSUserBundle的文章包中引用author字段。我是新手,请考虑一下。谢谢。 AliceBundle文档中的信息缺少此问题。

好这些是我的团队和用户灯具:

MB\MBBundle\Entity\Group:
Group1:
    __construct: ['Admins']
    __construct: ["ROLE_ADMIN"]
Group{2..7}:
    __construct(unique): [<randomGroup()>]
    __construct: [<randomRole()>]

MB\MBBundle\Entity\User:

User1:
    username: admin
    email: admin@local.com
    enabled: 1
    plainPassword: admin
    roles: [ROLE_ADMIN]
    groups: ['@Group1']

User{2..25}:
    username: <firstName()>
    email: <companyEmail()>
    enabled: <boolean(35)>
    plainPassword: <lexify()>
    roles: [<randomRole()>]
    groups: ['@Group*']

这是我的DataFixtures加载器类用户和组

<?php

namespace MB\MBBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Hautelook\AliceBundle\Alice\DataFixtureLoader;
use Nelmio\Alice\Fixtures;

class MBBundleFixtures extends DataFixtureLoader implements OrderedFixtureInterface
{

    protected function getFixtures()
    {
        return array (
            __DIR__.'/User.yml',
        );
    }

    public function rolesFixturesNames ()
    {

        return ['ROLE_ADMIN', 'ROLE_USER', 'ROLE_DIRECTORS', 'ROLE_TECHNICIANS', 'ROLE_OPERATORS', 'ROLE_ASISTANTS', 'ROLE_AUDITORS', 'ROLE_EXTERNALS'];
    }

    public function randomRole()
    {
        $names = ['ROLE_ADMIN', 'ROLE_USER', 'ROLE_PROFILE_ONE', 'ROLE_PROFILE_TWO'];

        return $names[array_rand($names)];
    }

    public function randomGroup()
    {
        $names = ['Users', 'Technicians', 'Operators', 'Asistants', 'Auditors', 'Externals'];

        return $names[array_rand($names)];
    } 

    public function getOrder()
    {
         return 1;
    }
}

对于要引用的实体,现在数据夹具就像:

MB\LegalBundle\Entity\LegalTitles:
  legalTitles{1..3}:
    category: <categoryTitlesNames()>
    name:  <text($maxNbChars = 5)>  <numberBetween($min = 100, $max = 999)>
    validFrom: <dateTimeBetween('-3 years', 'now')>
    validUntil: <dateTimeBetween($validFrom, '30 years')>
    validFor:  <numberBetween($min = 10, $max = 30)>
    geopointType: <geopointTypesNames()>
    latitude: <latitude($min = -5, $max = 15)>
    longitude: <longitude($min = 67, $max = 80)>
    elevation: <numberBetween($min = 50, $max = 4800)>
    dateCreated: null
    dateModified: null
    author: 

Loader类看起来像:

<?php 
namespace MB\LegalBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Hautelook\AliceBundle\Alice\DataFixtureLoader;
use Nelmio\Alice\Fixtures;

class LegalBundleFixtures extends DataFixtureLoader implements OrderedFixtureInterface
{
    protected function getFixtures()
    {
        return array (
            __DIR__.'/LegalTitlesFixtures.yml',
        );
    }

    public function categoryTitlesNames ()
    {
        $categoryMiningTitles = array (
            'CCON', 
            'LEXP',
            'RPP',
            'CVA',
        );
        return $categoryTitles [array_rand($categoryTitles)];
    }
    public function geopointTypesNames ()
    {
        $geopointTypes = array (
            'UnderGround Access', 
            'Surface Access', 
        );
        return $geopointTypes [array_rand($geopointTypes)];
    }

    public function getOrder()
    {
        return 2;
    }
}

如您所见,我想将MBBundle中的用户实体的作者引用到LegalBundle中的合法实体。再次感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您需要按特定顺序加载灯具。首先 - 将usersarticles灯具保存在单独的类/文件中。然后在DataFixtures类(加载yml个文件)中,您需要实现OrderedFixtureInterface类(Doctrine\Common\DataFixtures\OrderedFixtureInterface) - 只需一个方法 - getOrder

use Hautelook\AliceBundle\Alice\DataFixtureLoader;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;

class LoadUserData extends DataFixtureLoader implements OrderedFixtureInterface
{
    /**
     * {@inheritDoc}
     */
    protected function getFixtures()
    {
        return  array(
            __DIR__ . '/users.yml',
        );
    }

    /**
     * Get the order of this fixture
     *
     * @return integer
     */
    public function getOrder()
    {
        return 10;
    }
}

同样的文章:

use Hautelook\AliceBundle\Alice\DataFixtureLoader;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;

class LoadArticlesData extends DataFixtureLoader implements OrderedFixtureInterface
{
    /**
     * {@inheritDoc}
     */
    protected function getFixtures()
    {
        return  array(
            __DIR__ . '/articles.yml',
        );
    }

    /**
     * Get the order of this fixture
     *
     * @return integer
     */
    public function getOrder()
    {
        return 10;
    }
}

yml灯具中以这种方式引用用户:

article1:
    title: "Article titles"
    owner: @user1

有关该主题的更多文档:http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html#sharing-objects-between-fixtures