Symfony - object availability in Component
Im pretty sure this is my feeble mind not being able to cope with learning a language (PHP) and a framewor开发者_开发技巧k (Symfony) at the same time but ... I have a small stumbling block I would like to overcome. I grab some Doctrine Objects in an action - nothing major, pretty straightforward. I then use the template for that action to display the Object properties - again nothing unusual.
In the layout I include a component for some navigation - this component then makes a call to the Model and grabs the same Object as the previous action - but uses different properties from that object.
What I would like to do is share the Object that I created in the Action with the Component - what is the simplest way of doing this ? perhaps storing the Object in a separate class and reference its from both (ie some kind of singleton pattern approach) ? please help !
I guess that you could use a partial insted of a component. Imagen this scenario, you have a showAction of a certain User object, and you want to use a stadistics piece of template to show in all your application when the user is logged in.
First, you create the a executeShow and ashowSuccess.php template that could look something like:
action:
public function executeShow(sfWebRequest $request)
{//the session loggedin user, suppose he's logged in
$this->user = $this->getUser();
}
template:
[..blablabla...htmlcode]
<?php echo $user ?>
<?php include_partial('module/partial_name',array('user',$user))?>
[..blablabla...morehtmlcode]
Now, the partial called partial_name located in the template forlder of the "module" module, will spect a $user variable setted, that's the second argument of the include_partial is for, telling: "Define a variable called user with a value that is $user".
This way you can share the same instance of an object without having to re-search for the object on the database.
精彩评论