2017-02-09 99 views
4

的Symfony對ParamConverter手冊裏有這個例子:Symfony2 - 如何在控制器中使用@Entity標註?

/** 
* @Route("/blog/{post_id}") 
* @Entity("post", expr="repository.find(post_id)") 
*/ 
public function showAction(Post $post) 
{ 
} 

來源:http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#fetch-via-an-expression

但使用@Entity標註給了我這個錯誤。

The annotation "@Entity" in method AppBundle\Controller\CurrencyController::currencyAction() was never imported. Did you maybe forget to add a "use" statement for this annotation? 

顯然,我需要使用一個命名空間,但哪一個?請幫忙。

+1

你需要做什麼錯誤消息說 - 導入實體。您忘記在腳本的頂部添加「使用」語句。 –

+1

對,但是究竟是哪一個?這是個問題。 SensioFrameworkExtraBundle沒有@Entity註釋(至少不在Symfony2中),但手冊建議使用它。 –

回答

1

您正在嘗試使用ParameterConverter,因此此語法錯誤。

使用這個代替

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; 

/** 
* @Route("/blog/{post_id}") 
* @ParamConverter("post_id", class="VendorBundle:Post") 
*/ 
public function showAction(Post $post) 
{ 
} 

VendorBundle:Post應該以您的賣方所取代(如果有的話)和束。

4

Entity註釋僅存在於master(或未來v4)上。 Source file here

但是,正如您所看到的,這僅僅是@ParamConverter註釋的快捷方式,帶有expr選項,因此您必須在下一版本之前使用此註釋。

此致敬禮。

相關問題