2013-01-09 53 views
1

/編輯/從自定義類調用主義

我有這個類:

namespace Baza\BlogBundle\Form; 
use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Doctrine\ORM\EntityManager; 

class filterType extends AbstractType 
{ 

    protected $em; 
    public function __construct(EntityManager $em) 
{ 
    $this->em = $em; 
} 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 

    $this->$em->getDoctrine()->getEntityManager(); 

    /**** 
    ****/ 

} 
} 

這是我的服務陽明:

services: 
filterType: 
    class: Baza\BlogBundle\Form\filterType 
    arguments: [doctrine.orm.entity_manager] 

當我運行代碼,我得到以下例外:

Catchable Fatal Error: Argument 1 passed to Baza\BlogBundle\Form\filterType::__construct() must be an instance of Doctrine\ORM\EntityManager, none given

我完全沒有想法。

+0

嘗試:'arguments:[@ doctrine.orm.entity_manager]'添加了@,不知道它的含義,但是我所有的Symfony代碼都使用它。 – phpisuber01

+0

我試過了,我得到:「在掃描下一個令牌時,ScannerException發現字符@(64)無法啓動任何令牌」。 – Xardas

+0

確保您的空間數量正確。 –

回答

3

我自己創建了FormType。這應該工作:

<?php 
// Baza\BlogBundle\Form\filterType.php 

namespace Baza\BlogBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Doctrine\ORM\EntityManager; 

class filterType extends AbstractType 
{ 
    protected $em; 

    public function __construct(EntityManager $em) 
    { 
    $this->em = $em; 
    } 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
    // Do something with your Entity Manager using "$this->em" 
    } 

    public function getName() 
    { 
     return 'filter_type'; 
    } 
} 

在你的控制器使用類似

<?php 
// Baza\BlogBundle\Controller\PageController.php 

namespace Baza\BlogBundle\Controller; 
use Baza\BlogBundle\Form\filterType; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 

class BaseController extends Controller 
{ 
    public function testEntityManager() 
    { 
     // assign whatever you need 
     $enquiry = null; 
     // getEntityManager() is depricated. Use getManager() instead. 
     $em = $this->getDoctrine()->getManager(); 

     $this->createForm(
      new filterType($em), 
      $enquiry 
     ); 
    } 
} 

千萬不要忘了,包括/使用您正在使用的所有類。否則,PHP將假定該類在您當前使用的名稱空間內。

這就是爲什麼你得到了錯誤(CERAD上的帖子)

Catchable Fatal Error: Argument 1 passed to 
Baza\BlogBundle\Form\filterType::__construct() 
must be an instance of Baza\BlogBundle\Form\EntityManager [...] 

當你不包括EntityManager的PHP假定它是當前的命名空間這是Baza\BlogBundle\Form內部類。


有趣的類EntityManager50ecb6f979a07_546a8d27f194334ee012bfe64f629947b07e4919\__CG__\Doctrin‌​e\ORM\EntityManager是一個Doctrine2代理類。

由於Symfony的2.1,調用$this->getDoctrine()->getEntityManager()中其實表現就像當初EntityManager並且可以毫無問題地通過了Doctrine\ORM\EntityManager但代理類沒有lonoger結果。

+1

在我的示例代碼中,我使用了'$ this-> em'。看來你不小心使用了'$ this - > $ em'(帶有'em'前面的$),它尋找一個名爲'$ em'的變量並將其值作爲'$ this'的屬性名稱。由於您沒有定義'$ em',PHP嘗試訪問'$ this-> null'(或多或少)。要訪問一個對象的屬性,可以使用'$ object-> property',所以在這種情況下'$ this-> em'。 – flu

+0

問題解決了,再次感謝您。 – Xardas

1

需要@符號來表示參數是服務。然而,正如你發現的那樣,@跳過yaml解析器。解決方案是使用引號。

services: 
    filterType: 
     class: Baza\BlogBundle\Form\filterType 
     arguments: ['@doctrine.orm.entity_manager'] 

我記得我花了幾個小時才弄明白這一點。

+0

謝謝你的迴應。錯誤已更改爲:參數1傳遞給...filterType :: __ construct()必須是Baza \ BlogBu​​ndle \ Form \ EntityManager的實例,EntityManager50ec的實例... 919 \ __ CG __ \ Doctrine \ ORM \ EntityManager given'我在我的控制器中傳遞參數,如下所示:'$ em = $ this-> getDoctrine() - > getEntityManager(); $ form = $ this-> createForm(new filterType($ em),$ inquiry);'我錯過了什麼? – Xardas

+0

第一個錯誤是說我沒有傳遞任何參數:'none given',現在它說傳遞的參數是錯誤的:'EntityManager50ec ... 919 \ __ CG __ \ Doctrine \ ORM \ EntityManager給定'的實例。錯誤消息的第二部分是不同的。 – Xardas

+0

這是完整的錯誤信息:可捕獲的致命錯誤:傳遞給Baza \ BlogBu​​ndle \ Form \ filterType :: __ construct()的參數1必須是Baza \ BlogBu​​ndle \ Form \ EntityManager的一個實例,EntityManager50ecb6f979a07_546a8d27f194334ee012bfe64f629947b07e4919 \ __ CG __ \ Doctrine \ ORM \ EntityManager,在C:\ xampp \ htdocs \ baza \ src \ Baza \ BlogBu​​ndle \ Form \中定義,在C:\ xampp \ htdocs \ baza \ src \ Baza \ BlogBu​​ndle \ Controller \ PageController.php中調用, filterType.php line 18' – Xardas