2017-02-09 102 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选项,因此您必须在下一版本之前使用此注释。

此致敬礼。

相关问题