1. Fabrik 3.7 is now available. This version contains critical security updates, please update as soon as possible. Please remember to backup your site before updating, we also recommend testing your updates on development sites if possible.
  2. We had to restore the site around midday (CST) on Wed 7/26 from a backup approximately 8 hours old. So a few posts in the forums were lost. You may want to check to see if you need to re-post anything.

Control/debug a php cron plugin (in Schedule option)

Discussion in 'Professional Support' started by georgie, Sep 13, 2017 at 5:22 PM.

  1. georgie

    georgie Member

    Level: Professional
    Hello

    I would work with your excellent PHP cron plugin. I would load a long code, with many variables...

    Is it a way to "control" code inside (debug...)?
    To display somethings like "echo", "print_r", var_dump"...

    Thank you
     
  2. troester

    troester Well-Known Member Staff Member

    Level: Standard
    Yes, you can echo...
    but I think you'll need an exit; at least as last statement so you can see you outputs.
    Or install the 'dump' extension, it's very usefull for debugging. You can do
    dump($somevariable,'some remark');
    anywhere and you'll get the output in a pop up, not overridden by page loads so no need to add exit;




    Gesendet mit Tapatalk
     
    georgie and rackem like this.
  3. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    georgie likes this.
  4. georgie

    georgie Member

    Level: Professional
    OK, thanks.

    Hummm first please, without JDump extension, I can see an array from my list selected in a PHP schedule, just with this in the script, then running the schedule :
    Code (Text):
    // print_r($data); exit ;
    OK for me.
    Then to progress step-by-step, I would be able to recover my fields one-by-one. But I can not, doing this:
    Code (Text):
    var_dump($array["[MyTable___MyFieldA]"]); exit ;
    However "[MyTable___MyFieldA]" is well displayed in the array shows by "print_r($data)".

    Please can you help me to do this? Knowing that at term, I would be able to store each field/value in variables (to use it in a future query).

    Thanks in advance for all advices.
     
  5. troester

    troester Well-Known Member Staff Member

    Level: Standard
    var_dump ($data['mytable___myfield']);

    Gesendet mit Tapatalk
     
    georgie likes this.
  6. georgie

    georgie Member

    Level: Professional
    Thanks for this syntax, which seems better than mine.

    But no, I have always an "NULL" response.

    Please you see another thing to add?
     
  7. troester

    troester Well-Known Member Staff Member

    Level: Standard
    What do you get with var_dump ($data);
    array? object?

    Gesendet mit Tapatalk
     
    georgie likes this.
  8. georgie

    georgie Member

    Level: Professional
    I think it is a object in an array.

    Like this for example:
    Code (Text):
    Array ( [0] => Array ( [0] => stdClass Object ( [wosweb_users___id] => 1013
    [wosweb_users___id_raw] => 1013 [wosweb_users___name] => Mishel XXX
    [wosweb_users___name_raw] => Mishel xxx[wosweb_users___email] =>
    mishel.xxx@gmail.com [wosweb_users___email_raw] =>
    mishel.xxx@gmail.com [wosweb_users___username] => XXX...
     
  9. georgie

    georgie Member

    Level: Professional
    I think it is a object in an array.

    Like this for example:
    Code (Text):
    Array ( [0] => Array ( [0] => stdClass Object ( [wosweb_users___id] => 1013 [wosweb_users___id_raw] => 1013 [wosweb_users___name] => Mishel Makary [wosweb_users___name_raw] => Mishel Makary [wosweb_users___email] => mishel.everlastwellness@gmail.com [wosweb_users___email_raw] => mishel.everlastwellness@gmail.com [wosweb_users___username] => MMakary...
     
  10. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Yes, the $data in a PHP cron plugin is an array of arrays of objects. The outer array is groups, inner array is rows. Each row is an object. Even if your list is not "grouped", you'll still have that outer array, with just one entry.

    Typically the way you process it is ...

    Code (Text):

    foreach ($data as $group) {
       foreach ($group as $row) {
          // do stuff with $row->yourtable___yourelements for each row
       }
    }
     
    -- hugh
     
    georgie likes this.
  11. georgie

    georgie Member

    Level: Professional
    Yah! Great ! Thanks!
     
  12. georgie

    georgie Member

    Level: Professional
    Hello

    "an array of arrays of objects" OOOOk!

    My test works, I keep to work, but please some advices about my way, to verify that I am not in a bad direction :

    At term I would play a query to insert some data from my Fabrik list pointed by my Cron PHP plugin, into others tables from another DB (joined tables morever, why do it simple).
    I already verified my SQL query "in text", it works in DB.

    So now, I think I have just to replace my "text" in query by my variables recovered with "foreach".
    I think all will be OK, and easy, if my data would not be an array. But... it is an array, normal for a Fabrik list.

    But I do not know how "loop" a query, to execute them for each rows of my array.

    I imagine thing like this for now:
    Code (Text):

    <?php
    defined('_JEXEC') or die('Restricted access');

    // Create my variables, thanks to you
    foreach ($data as $group) {
       foreach ($group as $row) {
        $var1 = $row->TableA___Field1 ;
        $var2 = $row->TableA___Field2 ;
        $var3 = $row->TableB___Field1 ;
        $var4 = $row->TableC___Field1 ;
    ...
       }
    }

    // Then my query
    $db = FabrikWorker::getDBO(false, MyRemoteConnexionID);

    $query = "

    BEGIN;
    INSERT INTO MyTableA (MyField1, MyField2, ...)
    values ('$var1', '$var2', ...) ;

    INSERT INTO MyTableB (id, MyField1, ...)
    values (LAST_INSERT_ID(), '$var3', ...) ;

    INSERT INTO MyTableC (add_id, MyField1)
    values ( (SELECT MAX(id) from MyTableB) ,  '$var4', ...) ;
    COMMIT;

    " ;

    $db->setQuery($query);
    $db->query();
    ?>
     
    I go to do some tests in a test-DB, but if you have some advices... I take!

    Is it possible to "loop" a query for each rows from a list?

    Thanks you
     
  13. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Just put the actual queries in the loop.

    Code (Text):


    <?php
    defined('_JEXEC') or die('Restricted access');

    $db = FabrikWorker::getDBO(false, MyRemoteConnexionID);

    // Create my variables, thanks to you
    foreach ($data as $group) {
       foreach ($group as $row) {
        $var1 = $row->TableA___Field1 ;
        $var2 = $row->TableA___Field2 ;
        $var3 = $row->TableB___Field1 ;
        $var4 = $row->TableC___Field1 ;
    ...

          // Then my query

    $query = "

    BEGIN;
    INSERT INTO MyTableA (MyField1, MyField2, ...)
    values ('$var1', '$var2', ...) ;

    INSERT INTO MyTableB (id, MyField1, ...)
    values (LAST_INSERT_ID(), '$var3', ...) ;

    INSERT INTO MyTableC (add_id, MyField1)
    values ( (SELECT MAX(id) from MyTableB) ,  '$var4', ...) ;
    COMMIT;

    " ;

          $db->setQuery($query);
          $db->execute();
       }
    }


    ?>
     
     

Share This Page