简体   繁体   English

Symfony2 - 如何在控制器中使用@Entity注释?

[英]Symfony2 - How to use @Entity annotations in Controllers?

Symfony's manual on ParamConverter has this example: Symfony的ParamConverter手册有这个例子:

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

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

But using @Entity annotation gives me this error. 但是使用@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?

Obviously, I need to use a namespace, but which one? 显然,我需要使用命名空间,但哪一个? Please help. 请帮忙。

The Entity annotation only exist on master (or futur v4). Entity注释仅存在于master(或futur v4)上。 Source file here 源文件在这里

But as you can see, this is only a shortcut to @ParamConverter annotation with expr option, so you have to use this one until next release. 但正如您所看到的,这只是带有expr选项的@ParamConverter注释的快捷方式,因此您必须在下一个版本之前使用此注释。

Best regards. 最好的祝福。

You are trying to use ParameterConverter so this syntax is just wrong. 您正在尝试使用ParameterConverter因此这种语法错误。

Use this instead 请改用它

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 should be replaced with whatever your Vendor is (if any) and Bundle is. VendorBundle:Post应该替换为您的供应商(如果有的话)和Bundle。

Using annotation @ParamConverter with option repository_method is deprecated 不建议使用注释@ParamConverter和选项repository_method

The repository_method option of @ParamConverter is deprecated and will be removed in 6.0. Use the expr option or @Entity.

Thus it's better to use @Entity (documentation) 因此最好使用@Entity(文档)

You have to add the namespace : 您必须添加命名空间:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM