Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
$5
Calling a reusable function from a form or task
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.
This question has been answered.
webguy | 05/28/10 at 9:08pm
Edit
(5) Possible Answers Submitted...
See a chronological view of answers?
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
-

Last edited:
05/28/10
9:52pmBaruch Peña says: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, or performing operations) 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,Previous versions of this answer: 05/28/10 at 9:52pm
-

Last edited:
05/28/10
10:29pmFederico Marzocchi says: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-SymfonyPrevious versions of this answer: 05/28/10 at 10:29pm
-

Last edited:
05/28/10
10:36pmMartin Palacio says: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. -

Last edited:
06/01/10
6:57amJuG says:Hi,
Just create such a myTools 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;
}
}
Don't forget to clear the cache ! -

Last edited:
06/01/10
6:57amArturo Linares says: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:
Docs for eventsPrevious versions of this answer: 05/29/10 at 10:42am | 05/29/10 at 10:42am | 05/29/10 at 10:43am
This question has expired.
Current status of this question: Completed
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
