Symfony2 issues you do not understand

Share Button

In this page, we list some errors we met once, twice or more times, and on which we spent time to resolve.
We propose some solutions, sometimes great and sometimes quite ugly, but which worked for us.
We hope you will save some time by reading this post.

Feel free to come back, we update the page when we find a new error!

1) “404 Not Found” when running a functional test.

If you have a 404 status code when calling the code $this->client->request(‘GET’,'/some-path’), then you should generate an absolute URL to call :

// do not forget to extend Symfony\Bundle\FrameworkBundle\Test\WebTestCase
$url = $client->getContainer()->get('router')->generate('some-route',array(),true);
$this->client->request('GET', $url);

2) An exception has been thrown during the rendering of a template (“Notice: Undefined index: XXXX in /your/path/to/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php line XXX”) in AcmeBundle::template.html.twig at line XX.

This error is not very explicit. The most probably reason is you called a relation in a Doctrine query, for example “p.category” (get the products depending on a given category) and you have a problem in your relation definition.
You probably have defined the ManyToOne relation :

    // in the Product.php entity 
    // ...

    * @ORM\ManyToOne(targetEntity="Obtao\AcmeBundle\Entity\Category", inversedBy="products")
    * @ORM\JoinColumn(name="category_id",referencedColumnName="id")
    protected $category;

but you have forgotten to define the reverse OneToMany relation. So, to fix the problem, in our example we must add the following code in our Category.php :

    // in the Category.php entity 
    // ...

    * @ORM\OneToMany(targetEntity="Obtao\AcmeBundle\Entity\Product", mappedBy="category")
    protected $products;

3) Warning: spl_object_hash() expects parameter 1 to be object, string given in XXXX

The error is quite explicit : you have defined a string (or something else) instead of an object.

You need to check what is set in your main objet. If you use a form, you probably have a field type error (choice or text instead of entity for example). To solve this, you may need to use a Data Transformer

Share Button

2 thoughts on “Symfony2 issues you do not understand

  1. Thank you so much! I have some question.
    I got this error: Warning: spl_object_hash() expects parameter 1 to be object, string given
    Because I use this :
    * @var string
    * @ORM\OneToMany(targetEntity=”User”, mappedBy=”seeekers”, cascade={“persist”})
    * @ORM\JoinColumn(name=”user”, referencedColumnName=”id”)
    But I use this it works

    * @var string
    * @ORM\Column(name=”user”, type=”string”, length=255)
    Can you help fix this ?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Protected by WP Anti Spam