Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I get an entity from Doctrine Fixture reference?
    primarykey
    data
    text
    <p>I have added data fixtures in my project that relies on referencing entity objects from each other.</p> <p>In data fixture one, I have added entity references such as:</p> <pre><code> // GroupEntity_Fixtures.php file $this-&gt;addReference('GROUP_USER', $groupUser); $this-&gt;addReference('GROUP_ADMIN', $groupAdmin); </code></pre> <p>Where $groupAdmin and $groupUser are both Group() entities. In my second fixtures file I want to add those entities to my User entity via:</p> <pre><code> //UserEntity_Fixtures.php file $userActive-&gt;addGroup($this-&gt;getReference('GROUP_USER')); </code></pre> <p>$userActive is a User entity with a Many to Many relationship to Group Entity. Unfortunately it seems that I am only passing in a proxy of the entity and not the entity itself which renders the following error:</p> <pre><code> [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 </code></pre> <p>How do I convert the reference from a proxy to the entity it expects?</p> <hr> <p>Code for Group Fixture:</p> <pre><code>&lt;?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-&gt;setName('GROUP_USER'); $groupUser-&gt;setRole('ROLE_USER'); $manager-&gt;persist($groupUser); $groupAdmin = new Group(); $groupAdmin-&gt;setName('GROUP_ADMIN'); $groupAdmin-&gt;setRole('ROLE_USER,ROLE_ADMIN'); $manager-&gt;persist($groupAdmin); $manager-&gt;flush(); $this-&gt;addReference('GROUP_USER', $groupUser); $this-&gt;addReference('GROUP_ADMIN', $groupAdmin); } public function getOrder() { return 1; } } </code></pre> <p>Code for User Fixture</p> <pre><code>&lt;?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-&gt;setName('GROUP_USER'); $groupUser2-&gt;setRole('ROLE_USER'); $manager-&gt;persist($groupUser2); // This person represents an active (email verified) user. $userActive = new User(); $userActive-&gt;setPassword("passwordActive"); $userActive-&gt;setEmail("testActive@test.com"); $userActive-&gt;setUserName("testActive"); $userActive-&gt;setPassword(crypt($userActive-&gt;getPassword(),$userActive-&gt;getSalt())); $userActive-&gt;setEmailToken(md5(uniqid(rand(), true))); $userActive-&gt;addGroup($groupUser2); //$userActive-&gt;getGroups()-&gt;add($groupRepository-&gt;getGroupByName("BASIC_USER")); // This person represents an unactive (email not verified) user. $userUnactive = new User(); $userUnactive-&gt;setPassword("passwordUnactive"); $userUnactive-&gt;setEmail("testUnactive@test.com"); $userUnactive-&gt;setUserName("testUnactive"); $userUnactive-&gt;setPassword(crypt($userUnactive-&gt;getPassword(),$userUnactive-&gt;getSalt())); $userUnactive-&gt;setEmailToken(md5(uniqid(rand(), true))); // Persist objects into the database $manager-&gt;persist($userActive); $manager-&gt;persist($userUnactive); $manager-&gt;flush(); } public function getOrder() { return 2; } } </code></pre> <p>Code for Group Entity:</p> <pre><code>/** * @ORM\ManyToMany(targetEntity="User", inversedBy="groups") */ private $users; </code></pre> <p>Code for User Entity:</p> <pre><code>/** * @ORM\ManyToMany(targetEntity="Group", mappedBy="users") */ protected $groups; </code></pre> <p>Added Group Methos:</p> <pre><code>/** * Add groups * * @param \Blogger\BlogBundle\Entity\groups $groups * @return User */ public function addGroup(\Blogger\BlogBundle\Entity\groups $groups) { $this-&gt;groups[] = $groups; return $this; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload