當Symfony的2.8裝入燈具,我收到以下錯誤裝燈具時,我實現ContainerAwareInterface
:PHP致命錯誤:調用未定義的功能,比採用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"?
我覺得你忘記了'用戶()'之前''''。 –
如果我使用'$ fuser = new MeetingBundle \ Entity \ User();'我得到錯誤'[Symfony \ Component \ Debug \ Exception \ ClassNotFoundException]': '嘗試從命名空間「MeetingBundle \ DataFixtures \ ORM \ MeetingBundle \實體「'。 '您是否忘記了「使用」聲明「Symfony \ Component \ Security \ Core \ User \ User」或「Symfony \ Bridge \ Doctrine \ Tests \ Fixtures \ User」? – olga
如果我使用'$ fuser = new User();' Symfony \ Component \ Debug \ Exception \ ContextErrorException]','注意:未定義的變量:user',儘管我在頂部使用了MeetingBundle \ Entity \ User;' – olga