如何从Doctrine Fixture参考中获取实体


How can I get an entity from Doctrine Fixture reference?

我在项目中添加了依赖于相互引用实体对象的数据固定装置。

在数据固定装置一中,我添加了实体引用,例如:

            // GroupEntity_Fixtures.php file
    $this->addReference('GROUP_USER', $groupUser);
    $this->addReference('GROUP_ADMIN', $groupAdmin);

其中$groupAdmin和$groupUser都是Group()实体。在我的第二个fixture文件中,我想通过将这些实体添加到我的用户实体中

             //UserEntity_Fixtures.php file
             $userActive->addGroup($this->getReference('GROUP_USER'));

$userActive是一个与集团实体具有多对多关系的用户实体。不幸的是,我似乎只是传递了实体的代理,而不是实体本身,这会导致以下错误:

 [Symfony'Component'Debug'Exception'ContextErrorException]                    
  Catchable Fatal Error: Argument 1 passed to Blogger'BlogBundle'Entity'User:  
  :addGroup() must be an instance of Blogger'BlogBundle'Entity'groups, instan  
  ce of Proxies'__CG__'Blogger'BlogBundle'Entity'Group given, called in /home  
  /na/Practice/src/Blogger/BlogBundle/DataFixtures/ORM/CreateUserController_S  
  ignUpForm_UserEntity_Fixtures.php on line 27 and defined in /home/na/Practi  
  ce/src/Blogger/BlogBundle/Entity/User.php line 305 

如何将引用从代理转换为它所期望的实体?


成组夹具代码:

<?php
// DataFixtures/ORM/GroupEntity_Fixtrues.php
namespace Blogger'BlogBundle'DataFixtures'ORM;
use Doctrine'Common'DataFixtures'OrderedFixtureInterface;
use Doctrine'Common'DataFixtures'AbstractFixture;
use Doctrine'Common'Persistence'ObjectManager;
use Blogger'BlogBundle'Entity'User;
use Blogger'BlogBundle'Entity'Group;
class GroupEntity_Fixtures extends AbstractFixture implements OrderedFixtureInterface
{
    /**
     * {@inheritDoc}
     */
     public function load(ObjectManager $manager)
     {
        $groupUser = new Group();
        $groupUser->setName('GROUP_USER');
        $groupUser->setRole('ROLE_USER');
        $manager->persist($groupUser);
        $groupAdmin = new Group();
        $groupAdmin->setName('GROUP_ADMIN');
        $groupAdmin->setRole('ROLE_USER,ROLE_ADMIN');
        $manager->persist($groupAdmin);
        $manager->flush();
        $this->addReference('GROUP_USER', $groupUser);
        $this->addReference('GROUP_ADMIN', $groupAdmin);
     }
     public function getOrder()
     {
        return 1;
     }
}

用户固定装置代码

<?php
// DataFixtures/ORM/CreateUserController_SignUpForm_UserEntity_Fixtrues.php
namespace Blogger'BlogBundle'DataFixtures'ORM;
use Doctrine'Common'DataFixtures'OrderedFixtureInterface;
use Doctrine'Common'DataFixtures'AbstractFixture;
use Doctrine'Common'Persistence'ObjectManager;
use Blogger'BlogBundle'Entity'User;
use Blogger'BlogBundle'Entity'Group;
class CreateUserController_SignUpForm_UserEntity_Fixtures extends AbstractFixture implements OrderedFixtureInterface
{
    /**
     * {@inheritDoc}
     */
     public function load(ObjectManager $manager)
     {
        $groupUser2 = new Group();
        $groupUser2->setName('GROUP_USER');
        $groupUser2->setRole('ROLE_USER');
        $manager->persist($groupUser2);
        // This person represents an active (email verified) user.
        $userActive = new User();
        $userActive->setPassword("passwordActive");
        $userActive->setEmail("testActive@test.com");
        $userActive->setUserName("testActive");
        $userActive->setPassword(crypt($userActive->getPassword(),$userActive->getSalt()));
        $userActive->setEmailToken(md5(uniqid(rand(), true)));
        $userActive->addGroup($groupUser2);
        //$userActive->getGroups()->add($groupRepository->getGroupByName("BASIC_USER"));
        // This person represents an unactive (email not verified) user.
        $userUnactive = new User();
        $userUnactive->setPassword("passwordUnactive");
        $userUnactive->setEmail("testUnactive@test.com");
        $userUnactive->setUserName("testUnactive");
        $userUnactive->setPassword(crypt($userUnactive->getPassword(),$userUnactive->getSalt()));
        $userUnactive->setEmailToken(md5(uniqid(rand(), true)));
        // Persist objects into the database
        $manager->persist($userActive);
        $manager->persist($userUnactive);
        $manager->flush();
     }
     public function getOrder()
     {
        return 2;
     }
}

集团实体代码:

/**
 * @ORM'ManyToMany(targetEntity="User", inversedBy="groups")
 */
private $users;

用户实体代码:

/**
 * @ORM'ManyToMany(targetEntity="Group", mappedBy="users")
 */
protected $groups;

添加了组Methos:

/**
 * Add groups
 *
 * @param 'Blogger'BlogBundle'Entity'groups $groups
 * @return User
 */
public function addGroup('Blogger'BlogBundle'Entity'groups $groups)
{
    $this->groups[] = $groups;
    return $this;
}

addGroup方法具有错误的类型提示:

应该是:

/**
 * Add groups
 *
 * @param 'Blogger'BlogBundle'Entity'Group $groups
 * @return User
 */
public function addGroup('Blogger'BlogBundle'Entity'Group $groups)
{
    $this->groups[] = $groups;
    return $this;
}

注意'Blogger'BlogBundle'Entity'Group而不是'Blogger'BlogBundle'Entity'groups