2016-08-15 112 views
-1

當Symfony的2.8裝入燈具,我收到以下錯誤裝燈具時,我實現ContainerAwareInterfacePHP致命錯誤:調用未定義的功能,比採用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"? 
+1

我覺得你忘記了'用戶()'之前''''。 –

+0

如果我使用'$ 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

+0

如果我使用'$ fuser = new User();' Symfony \ Component \ Debug \ Exception \ ContextErrorException]','注意:未定義的變量:user',儘管我在頂部使用了MeetingBundle \ Entity \ User;' – olga

回答

-1

此:

 $fuser = User(); 

use MeetingBundle\Entity\User; 

有一個在沒有User功能10名稱空間。它可能在MeetingBundle\Entity\User,所以你可能想要

$fuser = MeetingBundle\Entity\User\User(); 

改爲。

+0

使用你的建議'$ fuser = MeetingBundle \ Entity \ User;'給出'Error:Undefined constant'MeetingBundle \ DataFixtures \ ORM \ MeetingBundle \ Ent ity \ User'' – olga

+0

使用'$ fuser = MeetingBundle \ Entity \ User );''給出相同的'[Symfony \ Component \ Debug \ Exception \ UndefinedFunctionException]' :'嘗試從命名空間「MeetingBundle \ DataFixtures \ ORM \ MeetingBundle \ Entity」調用函數「User」。 – olga

+0

但是,錯誤,如果我不從'ContainerAwareInterface'擴展。如何擺脫這個錯誤?不過,我需要容器的密碼和其他東西。一般來說,我使用的代碼遵循官方文檔示例,並且在問題中給出了鏈接。 – olga

1

首先,你忘記了new運算符User()之前,因此PHP將它視爲一個函數調用。

改變這一行:

$fuser = User(); 

到:

$fuser = new User(); 

而且你使用不符合存在$user變量:

$encodedPsswd = $encoder->encodePassword($user, 'pswd'.$i); 

你應該改變它使用$fuser

$encodedPsswd = $encoder->encodePassword($fuser, 'pswd'.$i); 
+0

我很抱歉,似乎我忘了粘貼它的問題。 'new'在我的代碼中,它不起作用。感謝您關注'$ fuser'而不是'$ user'。 ANYWAY,糾正這一點,stil給出了同樣的錯誤,並且由於我添加了這個問題,命名空間錯誤源於其他代碼。 – olga

相關問題