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

时间:2013-06-23 20:00:45

标签: php mysql symfony orm doctrine-orm

我在项目中添加了数据夹具,它依赖于相互引用实体对象。

在数据夹具1中,我添加了实体引用,例如:

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

$ groupAdmin和$ groupUser都是Group()实体。在我的第二个灯具文件中,我想通过以下方式将这些实体添加到我的用户实体中:

             //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 

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


Group Fixture代码:

<?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;

添加了Group Methos:

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

    return $this;
}

1 个答案:

答案 0 :(得分:3)

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 \ BlogBu​​ndle \ Entity \ Group”而不是“\ Blogger \ BlogBu​​ndle \ Entity \ groups”