0

i edited the post

where i set $appartement:

$appartement = new Apartement ();
$user=1;
$em = $this->getDoctrine()->getManager();
$appartement = $em->getRepository('LeymaxDashboardBundle:Apartement')->find(1);

well i think that i have a syntax error that what i have in the controller that send s params to twig file :

return $this->render('LeymaxDashboardBundle:Default:client.html.twig', array('apart' => $appartement,
                                                                            'user' => $connected_user->getId(),
                                                                            ));

in the twig file i do the following :

{{ path('leymax_changemodules_showModal',{'iduser': user,'profil':'client','apart':apart}) }}

the generated route is like so :

leymax_changemodules_showModal:
pattern:  /changemodulesModal/{iduser}/{profil}/{apart}
defaults: { _controller: LeymaxDashboardBundle:Default:changeModulesModal }

the error message :

An exception has been thrown during the rendering of a template ("Warning: preg_match() expects parameter 2 to be string, object given 

what's wrong i'm doing ?

3
  • 1
    what about checking it yourself? Commented Dec 10, 2013 at 14:55
  • i'm in the helping-mood today, but you're right @WouterJ ... i probably shouldn't encourage people to ask more questions of this type by answering them :) Commented Dec 10, 2013 at 15:02
  • Please add the code where you set $user and $apart and the exception message + stacktrace ... Commented Dec 10, 2013 at 15:35

1 Answer 1

1

You can access a (public) property or method of an object in twig using the . syntax.

As long as this method's return value or property is of type string/integer - the following will work:

{{ 
    path(
       'route_name',
       {
           'parameter':  object.property, 
           'parameter2': object.someMethod 
       }
   ) 
}}

You can further add a _toString() method to the object's class that will return a i.e. the id ...

class Object {

    public function __toString()
    {
        return $this->id;
    }

... and then pass the object directly:

{{ 
    path(
       'route_name',
       {
           'id':  object
       }
   ) 
}}
Sign up to request clarification or add additional context in comments.

3 Comments

well i thank you but i think that's not the issue , i would like to pass the whole object in the path. but the response is : preg_match() expects parameter 2 to be string object given
you can aswell pass the whole object as parameters. There is no need to json_encode. Twig creates an Array from that json syntax provided as the path methods 2nd argument and passes it to the generateUrl of the @router. Where are you stuck?
If apart is an object you either have to add a __toString() method or query the method / property i.e. apart.name that shall be passed to the route-generation method. All described in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.