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

Discussion in 'Professional Support' started by georgie, Sep 13, 2017.

  1. georgie

    georgie Member

    Level: Community
    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: Professional
    georgie likes this.
  4. georgie

    georgie Member

    Level: Community
    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: Community
    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: Community
    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: Community
    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: Professional
    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: Community
    Yah! Great ! Thanks!
     
  12. georgie

    georgie Member

    Level: Community
    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: Professional
    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();
       }
    }


    ?>
     
     
    georgie likes this.
  14. georgie

    georgie Member

    Level: Community
    Hello

    Hummm... Thanks it seems be in the right way, but as it, it does not work.
    An error appears:
    However when I do a var_dump for my query, she is full OK (but just one query is displayed).
    If I copy-paste this query generated in DB, SQL tab : full OK, executed!

    Hummm I do not understand...:mad:

    I suspect the sequence of queries "INSERT INTO" with "BEGIN" and "COMMIT" to be responsible for a PHP-related error, but I can not see it (or I do not know how).

    If I just delete "BEGIN" and "COMMIT" (keeping all my "INSERT INTO"), no changes, it does not work.
    If I do just one "INSERT INTO" with "BEGIN" and "COMMIT", no changes, it does not work.
    If I do just one "INSERT INTO" without "BEGIN" and "COMMIT", so OK, it works.

    I think it is the loop which does not manage the "BEGIN" and "COMMIT".

    Please have you an idea?:p

    Here my full code:
    Code (Text):
    <?php

    defined('_JEXEC') or die('Restricted access');

    // Variables
    foreach ($data as $group) {
       foreach ($group as $row) {
     
         $wos_title = $row->profiles___title_raw ;
         $wos_firstname = $row->profiles___firstname_raw ;
         $wos_lastname = $row->profiles___lastname_raw ;
         $wos_email = $row->wosweb_users___email_raw ;
         $wos_country = $row->address___country ;
         $wos_code_phone1 = $row->telephones___code_phone1 ;
         $wos_phone1 = $row->telephones___phone1 ;

    // Query
    $db = FabrikWorker::getDBO(false, 3);

    $query = "

    BEGIN;

    INSERT INTO customer (civility, lastname, firstname, email)
    VALUES ('$wos_title', '$wos_lastname', '$wos_firstname', '$wos_email') ;

    INSERT INTO address (id_customer, country)
    VALUES (LAST_INSERT_ID(), '$wos_country') ;

    INSERT INTO telephone (id_customer, id_address, code_tel1, tel1)
    VALUES ( (SELECT MAX(id_customer) FROM customer) , (SELECT MAX(id_address) from address) , '$wos_code_phone1', '$wos_phone1') ;

    INSERT INTO customer_organisme (id_customer, id_organisme)
    VALUES ( (SELECT MAX(id_customer) FROM customer) , '1') ;

    COMMIT;

    " ;

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

       }

    }

    ?>

     
     
  15. georgie

    georgie Member

    Level: Community
    Hello

    I think I have an idea,or just a clue, but it is finaly another discuss.

    I open a new discuss, this is solved!

    THX
     
    cheesegrits likes this.

Share This Page