I have added data fixtures in my project that relies on referencing entity objects from each other.
In data fixture one, I have added entity references such as:
// GroupEntity_Fixtures.php file
$this->addReference('GROUP_USER', $groupUser);
$this->addReference('GROUP_ADMIN', $groupAdmin);
Where $groupAdmin and $groupUser are both Group() entities. In my second fixtures file I want to add those entities to my User entity via:
//UserEntity_Fixtures.php file
$userActive->addGroup($this->getReference('GROUP_USER'));
$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:
[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
How do I convert the reference from a proxy to the entity it expects?
Code for 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;
}
}
Code for User Fixture
<?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("[email protected]");
$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("[email protected]");
$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;
}
}
Code for Group Entity:
/**
* @ORM\ManyToMany(targetEntity="User", inversedBy="groups")
*/
private $users;
Code for User Entity:
/**
* @ORM\ManyToMany(targetEntity="Group", mappedBy="users")
*/
protected $groups;
Added 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;
}
The addGroup
method has the wrong type hint:
It should be:
/**
* Add groups
*
* @param \Blogger\BlogBundle\Entity\Group $groups
* @return User
*/
public function addGroup(\Blogger\BlogBundle\Entity\Group $groups)
{
$this->groups[] = $groups;
return $this;
}
Notice \Blogger\BlogBundle\Entity\Group
instead of \Blogger\BlogBundle\Entity\groups
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With