Search code examples
phpsymfonytwigsonata

Controller attributes cannot contain non-scalar\/non-null values


I would like to provide my controller object like this with hinclude.js :

{{ render_hinclude(controller("AppBundle:Default:Test"), { 'object': my_object }) }}

Unfortunately I got this error:

"An exception has been thrown during the rendering of a template (\"Controller attributes cannot contain non-scalar/non-null values (value for key \"my_object\" is not a scalar or null).\")

Can you help me ?

Thanks in advance !


Solution

  • The exception is right, only scalar/null values are allowed to pass for controller attributes when render_hinclude() is used, because is needed to generate a valid fragment URI from them.


    Why?

    Suppose you try to pass this entity with private properties, when it comes to generate the URI for this parameter happens the following:

    var_dump(http_build_query(array('object' => $my_object)))
    
    // output: 
    // string(0) ""
    

    http://php.net/manual/en/function.http-build-query.php#refsect1-function.http-build-query-parameters

    If query_data is an object, then only public properties will be incorporated into the result.


    BTW the statement that you provided is not valid, you should pass this attribute to controller(...) function, something like this:

    {{ render_hinclude(controller("AppBundle:Default:test", {'id': my_object.id })) }}
    

    Then, your controller should have the scalar value id and get the corresponding object. However, use the argument resolver feature to avoid this step:

    public function testAction(Object $object) {...}
    

    Next:

    {{ render_hinclude(controller("AppBundle:Default:test", {'object': my_object.id })) }}