Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Trigger a method through oil and pass a variable
  • Hi, I have some problems with tasks under oil. I have to collect some files (.xml) from a folder, parse them, take the informations, insert them in a mysql database, and then move the files in an archive folder. If I do it through the browser, it works perfect, but I need to do this through oil. So, here is my code : <code>
    <?php namespace Fuel\Tasks; class Trigger
    { public static function run()
    {
    //read all the XML files (only XML type)
    $opta_files = \File::read_dir( DOCROOT.'public/opta_archive', 0, array( '\.xml$' => 'file') ); if(isset($opta_files[0]))
    {
    foreach ($opta_files as $opta_file)
    {
    $file_name = basename($opta_file); $count = 1; while( file_exists(DOCROOT.'public/tmp_fantomic'.DS.date('YmdHms') . '_' . $count .'_'. $file_name) )
    {
    $count++;
    } \File::rename(DOCROOT.'public/opta_archive'.DS.$file_name ,
    DOCROOT.'public/tmp_fantomic'.DS.date('YmdHms') . '_' . $count .'_'. $file_name);
    } //run a method (placed also inside the task file)
    \Response::redirect('trigger/run_method'); } public static function run_method()
    {
    //read all the XML files (only XML type)
    $tmp_files = \File::read_dir( DOCROOT.'public/tmp_fantomic', 0, array( '\.xml$' => 'file') );
    if(isset($tmp_files[0]))
    { foreach ($tmp_files as $tmp_file)
    {
    $file_name = basename($tmp_file, '.xml'); \Session::set('tmp_file', $tmp_file); //doesn't work :( //explode the name of the file
    $pieces = explode("-", $file_name); switch (end($pieces))
    {
    case 'matchresults': /* How can i trigger a method and also pass a variable
    * Method name is : parse_match_events,
    * I want to pass this variable : $tmp_file
    */ $this->parse_venues($tmp_file); //DOESN'T WORK break; }
    }
    }
    } public static function parse_venues($tmp_file)
    {
    echo $tmp_file;
    } }
    </code> My question is:
    After i trigger the method <quote>run_method</quote>, inside i execute some code, and then I have to trigger another method (named <quote>parse_venues</quote>, but to pass also a variable. Ho can I do that through oil ??? I was trying to set a Session variable, to hold the value I want to pass it, but when I read the Session isn't inside :( \Session::set('tmp_file', $tmp_file); >>> doesn't work Please help me, because I really don't know how to do it ! Thanks.
  • You're running a commandline PHP script, so there is no browser. Which means that things like Sessions, Reponses, redirects, etc, don't work. I don't see why calling another method in your class won't work. Altough you call it using $this while in a static context, so $this does not exist (there is no object). Use 'static::parse_venues($var)' instead. A task only has a single run method. If your task has multiple functions that need to be called individually from the commandline, you have to use parameters. Check core/tasks/session.php for an example on how that is done.
  • This is exactly what I need to know : 'static::parse_venues($var)' I didn't know hot to trigger another method from inside the run() method,
    and also to pass a value, to the new triggered method, in order to use it inside. Many thanks for your complete answer and also for the good response time.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion