在Symfony 2.8中加载灯具时,我在实施ContainerAwareInterface
时遇到以下错误:
PHP Fatal error: Call to undefined function MeetingBundle\DataFixtures\ORM\User()
如果我不使用此界面,则不会出现此错误。我从官方Symfony documentation获取了示例代码。
文件:
<?php
namespace MeetingBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use MeetingBundle\Entity\User;
//C:\Bitnami\wampstack-5.6.20-0\apache2\htdocs\sym_prog\just2\src\MeetingBundle\DataFixtures\ORM\LoadUsers01Loader.php
/*
Class LoadUsers01
@package Meeting\DataFixtures\ORM
*/
class LoadUsers01Loader extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface {
/**
* @var ContainerInterface
*/
private $container;
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
/*
{@inheritDoc}
*/
public function load(ObjectManager $manager)
{
for ($i = 1; $i <= 10; $i++) {
$fuser = new User();
$fuser->setUsername('name'.$i);
$fuser->setEmail('g.statkute'.$i.'@gmail.com');
$fuser->setIsActive(1); //also tried to use true
$encoder = $this->container->get('security.password_encoder');
$encodedPsswd = $encoder->encodePassword($user, 'pswd'.$i);
//$fuser->setPassword('pswd'.$i);
$fuser->setPassword($encodedPsswd);
$manager->persist($fuser);
$this->addReference('fuser:name'.$i, $fuser);
}
$manager->flush();
}
public function getOrder()
{
return 1;
}
}
完整错误:
c:\Bitnami\wampstack-5.6.20-0\apache2\htdocs\sym_prog\just2>php app/console doctrine:fixtures:load
Careful, database will be purged. Do you want to continue y/N ?y
> purging database
> loading [1] MeetingBundle\DataFixtures\ORM\LoadUsers01
PHP Fatal error: Call to undefined function MeetingBundle\DataFixtures\ORM\User() in C:\Bitnami\wampstack-5.6.20-0\apache2\htdocs\sym_prog\just2\src\MeetingBundle\DataFixtures\ORM\LoadUsers01.php on line 40
我不知道ContainerAwareInterface
如何导致此错误?如果我没有实现它并使用没有编码器的普通密码,则夹具会加载到数据库中。
实际上,忽略此错误并进一步工作,我开始得到其他命名空间错误。例如,symfony尝试从命名空间“MeetingBundle \ Entity \ Doctrine \ Common \ Collections”加载类“ArrayCollection”。命名空间有问题。要解决这个问题吗?
src\MeetingBundle\Entity\User.php
namespace MeetingBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
/**
* User
*
* @ORM\Table(name="tuser")
* @ORM\Entity(repositoryClass="MeetingBundle\Repository\UserRepository")
*/
class User implements AdvancedUserInterface, EquatableInterface, \Serializable
<...>
/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
*
*/
private $roles;
public function __construct() {
$this->roles = new Doctrine\Common\Collections\ArrayCollection();
<...>
创建用户时会出现错误:
Attempted to load class "ArrayCollection" from namespace "MeetingBundle\Entity\Doctrine\Common\Collections".
Did you forget a "use" statement for "Doctrine\Common\Collections\ArrayCollection"?
答案 0 :(得分:1)
首先,您在new
之前忘记了User()
运算符,因此PHP将其视为函数调用。
更改此行:
$fuser = User();
到:
$fuser = new User();
此外,您正在使用行中不存在的$user
变量:
$encodedPsswd = $encoder->encodePassword($user, 'pswd'.$i);
您应该将其更改为使用$fuser
:
$encodedPsswd = $encoder->encodePassword($fuser, 'pswd'.$i);
答案 1 :(得分:-1)
此:
$fuser = User();
和
use MeetingBundle\Entity\User;
User
命名空间中没有MeetingBundle\DataFixtures\ORM
个函数。它可能在MeetingBundle\Entity\User
结束,所以你可能想要
$fuser = MeetingBundle\Entity\User\User();
代替。