找不到对象但是在" new"

时间:2018-05-19 09:56:17

标签: symfony type-hinting symfony-3.3

它在工作:

use AppBundle\Entity\Product;
use AppBundle\Entity\ProductPriceAccept;

public function editAction(Product $product)
{
   $ppa = new ProductPriceAccept();
   // further some operations on $product
}

但它并不想工作:

use AppBundle\Entity\Product;
use AppBundle\Entity\ProductPriceAccept;

public function editAction(Product $product, ProductPriceAccept $ppa)
{
   $ppa->setPrice();
   // further some operations on $product
}

我明白了:

AppBundle\Entity\ProductPriceAccept object not found.

Entity \ ProductPriceAccept.php是:

// src/AppBundle/Entity/ProductPriceAccept.php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="Product_Price_Accept")
 */
class ProductPriceAccept
{
   // ...
}

我有点困惑,因为通常类型提示对我来说效果很好,而且你可以看到它在产品实体的类似情况下工作。第一个解决方案工作正常但我想知道第二个解决方案是如何产生问题的。我清除缓存,检查错别字。不知道什么可以做更多。

1 个答案:

答案 0 :(得分:4)

如果要读取一个参数,它可以自动读取存储库。

不止一个,需要一些帮助。

  

id选项指定路由中的哪个占位符传递给使用的存储库方法。如果未指定存储库方法,则默认使用find()。

     

这也允许您在一个操作中拥有多个转换器:

因此,对于ProductPriceAccept,您需要一个明确的@ParamConverter - 您可能还需要use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;行。

/**
 * @Route("/blog/{id}/comments/{comment_id}")
 * @ParamConverter("comment", class="SensioBlogBundle:Comment", 
 *     options={"id" = "comment_id"})
 */
public function showAction(Post $post, Comment $comment)
{
}

来自:@ParamConverter