Search code examples
symfonytype-hintingsymfony-3.3

Object not found but working when created by "new"


It's working:

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

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

But it doesn't want to work:

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

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

I get:

AppBundle\Entity\ProductPriceAccept object not found.

Entity\ProductPriceAccept.php is:

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

use Doctrine\ORM\Mapping as ORM;

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

I'm a little confused because usually type hint works well for me and as you can see up it works in similar situation for Product Entity. First solution works fine but I want to find out how the second makes problem. I cleared cache, checked typos. No idea what can do more.


Solution

  • With a single parameter to be read, it can automatically read the repository.

    With more than one, it needs some help.

    The id option specifies which placeholder from the route gets passed to the repository method used. If no repository method is specified, find() is used by default.

    This also allows you to have multiple converters in one action:

    So, you will need an explicit @ParamConverter for ProductPriceAccept - you will also likely need use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; line.

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

    from: @ParamConverter.