Question

I'm currently working on a project for my on the job trainning. But I'm in front of an error which and can't resolve.

So, I use a relation ManyToOne to join 2 things, products in category.

But when I want to add a product I have this error :

ContextErrorException: Catchable Fatal Error: Object of class Sam\VitrineBundle\Entity\Categorie could not be converted to string in D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\src\Sam\VitrineBundle\Entity\Produit.php line 124

My controller, code where I have the error.

...
$catRepo = $this->getDoctrine()->getManager()->getRepository('SamVitrineBundle:Categorie');
$cat = $catRepo->findOneById($form["categorie"]->getData());
$product -> setCategorie($cat);
...

My entity, Product

...
    /**
     * @ORM\ManyToOne(targetEntity="Categorie", inversedBy="produits", cascade={"remove"})
     * @ORM\JoinColumn(name="categorie_id", referencedColumnName="id")
     */
    protected $categorie;

    /**
     * Set categorie
     *
     * @param Sam\VitrineBundle\Entity\Categorie $categorie
     */
    public function setCategorie(\Sam\VitrineBundle\Entity\Categorie $categorie)
    {
      $this->$categorie = $categorie;
    }

    /**
     * Get categorie
     *
     * @return Sam\VitrineBundle\Entity\Categorie 
     */
    public function getCategorie()
    {
      return $this->categorie;
    }
...

I'm sorry, some variable are in French. categorie -> category produit -> product

If someone can help me. Ask me if you want more informations, but I give you the main informations which I think the error comes from.

[1] Symfony\Component\Debug\Exception\DummyException: 
    at n/a
        in D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\vendor\symfony\symfony\src\Symfony\Component\Debug\ErrorHandler.php line 174

    at Symfony\Component\Debug\ErrorHandler->handle('4096', 'Object of class Sam\VitrineBundle\Entity\Categorie could not be converted to string', 'D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\src\Sam\VitrineBundle\Entity\Produit.php', '124', array('categorie' => object(Categorie)))
        in D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\src\Sam\VitrineBundle\Entity\Produit.php line 124

    at Sam\VitrineBundle\Entity\Produit->setCategorie(object(Categorie))
        in D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\src\Sam\VitrineBundle\Controller\VitrineController.php line 113

    at Sam\VitrineBundle\Controller\VitrineController->addProductAction()
        in  line 

    at call_user_func_array(array(object(VitrineController), 'addProductAction'), array())
        in D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\app\bootstrap.php.cache line 2925

    at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
        in D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\app\bootstrap.php.cache line 2897

    at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
        in D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\app\bootstrap.php.cache line 3036

    at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
        in D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\app\bootstrap.php.cache line 2304

    at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
        in D:\DOCUMENTS\Dropbox\COURS\S4\STAGE\Symfony\web\app_dev.php line 28
Was it helpful?

Solution

In your entity

public function setCategorie(\Sam\VitrineBundle\Entity\Categorie $categorie)
{
    $this->categorie = $categorie;
}

instead of

public function setCategorie(\Sam\VitrineBundle\Entity\Categorie $categorie)
{
    $this->$categorie = $categorie;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top