本文介绍了如何从Doctrine Fixture参考获取实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在数据夹具中,我添加了一些实体引用,例如: // GroupEntity_Fixtures.php文件 $ this-> addReference('GROUP_USER',$ groupUser); $ this-> addReference('GROUP_ADMIN',$ groupAdmin); 其中$ groupAdmin和$ groupUser都是Group()实体。在我的第二个夹具文件中,我想通过以下方式将这些实体添加到我的用户实体: //UserEntity_Fixtures.php文件 $ userActive-> addGroup($ this-> getReference('GROUP_USER')); $ userActive是一个与组实体有多对多关系的用户实体。不幸的是,我只是传递一个实体的代理,而不是实体本身,它呈现以下错误: Symfony\Component\Debug\Exception\ContextErrorException] 可追踪致命错误:参数1传递给Blogger\BlogBundle\Entity\User::addGroup()必须是Blogger \BlogBundle\Entity\groups,instan ce of Proxies\__CG__\\BBger_\\BlogBundle\Entity\Group给出,在/ home / na / Practice / src中调用/ Blogger / BlogBundle / DataFixtures / ORM / CreateUserController_S ignUpForm_UserEntity_Fixtures.php在第27行并定义在/ home / na / Practi ce / src / Blogger / BlogBundle / Entity / User.php line 305 如何将代理引用转换为预期的实体? 组装夹具代码: // DataFixtures / ORM / GroupEntity_Fixtrues.php 命名空间Blogger\BlogBundle\DataFixtures\ORM; 使用Doctrine\Common\DataFixtures\OrderedFixtureInterface; 使用Doctrine\Common\DataFixtures\AbstractFixture; 使用Doctrine\Common\Persistence\ObjectManager; 使用Blogger\BlogBundle\Entity\User; 使用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 命名空间Blogger\BlogBundle\DataFixtures \ORM; 使用Doctrine\Common\DataFixtures\OrderedFixtureInterface; 使用Doctrine\Common\DataFixtures\AbstractFixture; 使用Doctrine\Common\Persistence\ObjectManager; 使用Blogger\BlogBundle\Entity\User; 使用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); //这个人代表一个活跃的(电子邮件验证的)用户。 $ 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)); //此人表示未激活(电子邮件未验证)用户。 $ 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))); //将对象保存到数据库 $ 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: / ** *添加组 * * @param \Blogger\BlogBundle\Entity\groups $ groups * @return用户 * / public function addGroup(\Blogger\BlogBundle\Entity\groups $ groups) { $ this-> groups [] = $ groups ; return $ this; } 解决方案 addGroup方法错误类型提示: 应该是: / ** *添加组 * * @param \Blogger\BlogBundle\Entity\Group $ groups * @return用户 * / public function addGroup(\Blogger\BlogBundle\Entity\Group $ groups) { $ this-> groups [] = $ groups; return $ this; } 通知\Blogger\BlogBundle\Entity\Group的\Blogger\BlogBundle\Entity\groups 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 305How do I convert the reference from a proxy to the entity it expects?Code for Group Fixture:<?php// DataFixtures/ORM/GroupEntity_Fixtrues.phpnamespace 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.phpnamespace 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" 这篇关于如何从Doctrine Fixture参考获取实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-26 13:43