Ask your Symfony questions! Pay money and get answers fast! (more info)

Calling a reusable function from a form or task Symfony

  • SOLVED

Hi,

I am using symfony 1.3 and doctrine 1.2.

I have a situation where, when an object's value is changed from what is in the database, I need to do a function.

I need to be able to call this function from my form and from a task or anywhere else. So, my question is, (1) where do I put this function for best reusability (2) how do i call it from my form (3) how do i call it from a task.

I currently want to call this function via the form doing something like:

/lib/form/doctrine/myForm.class.php

class myForm extends BaseMyForm {
protected function updateXXXColumn($value)
{
if($this->getObject()->getXXX() != value)
{
// I want the action to go here, something like
// myfunction($this->getObject()->getId());
}
return $value;
}
}


but the function is embedded in the form, and i want it to be reusable by a task or from an action.

Answers (5)

2010-05-29

JuG answers:

Hi,

Just create such a <em>myTools</em> class in /your/project/lib directory :


<?php

// ./lib/myTools.class.php


class myTools
{
static public function doSomething( $id )
{
/* ... */
}

static public function anotherReusableFunction( $foo )
{
/* ... */
}


static public function andMore( $bar )
{
/* ... */
}

}





You will then be able to use it anywhere thanks to symfony autoloading :



class myForm extends BaseMyForm {

protected function updateXXXColumn($value)

{

if($this->getObject()->getXXX() != value)

{

// I want the action to go here, something like

myTools::doSomething($this->getObject()->getId());

}

return $value;

}

}



<em>Don't forget to clear the cache !</em>

2010-05-29

Arturo Linares answers:

This is a good example for events. It keeps clean the code and you can plug more than 1 listener :)

After updating the value, you can do something like:


public function updateXXXColumn($value)
{
...

$dispatcher = ProjectConfiguration::getActive()->getEventDispatcher();

// Notify the dispatcher of the end of the fetch process
$dispatcher->notify(new sfEvent($this, 'custom_events.myform_updated', array(
'uri' => $uri,
'parameters' => $parameters,
'result' => $result
)));
}


to subscribe an event listener (the code you want to execute):

// replace 'callback' to a valid callback. It can be a function, class method, etc.
$dispatcher->connect('custom_events.myform_updated', 'callback');


For more info:
[[LINK href="http://www.symfony-project.org/gentle-introduction/1_4/en/17-Extending-Symfony"]]Docs for events[[/LINK]]

2010-05-28

Baruch Peña answers:

hi

You can use a helper in order to define a function an use it later on in actions or templates.

http://www.symfony-project.org/book/1_2/07-Inside-the-View-Layer

"Helper functions (regular PHP functions returning HTML code, <strong>or performing operations</strong>) should be saved in a file called FooBarHelper.php, where FooBar is the name of the helper group. Store the file in the apps/frontend/lib/helper/ directory (or in any helper/ directory created under one of the lib/ folders of your project) so it can be found automatically by the use_helper('FooBar') helper for inclusion."

and if you are in action you can include your helper with:

sfLoader::loadHelpers(array('FooBar'));

regards,

2010-05-28

Federico Marzocchi answers:

A solution might be to declare that function as a static method in an class, so that you can take advantage of autoloading.

However, if you need to act on objects when their state is changed in various places, from forms to actions to tasks, you might want to consider using events

http://www.symfony-project.org/book/1_2/17-Extending-Symfony

2010-05-28

Martin Palacio answers:

I think the best way is to create a "worker class" (ex. myUtils.class.php) and place it under the /lib project folder (ex. /lib/util). Then, your function will become a static method of that class.
Another -more advanced- approach, is to use the symfony event system. In your example, you can notify an event, define your function as a handler for that event.

Nevertheless, first you need to know where that logic belongs to: I mean, the M, V or C layer.