Discover User ID after form process

Discussion in 'Standard Support' started by ourblok, Sep 12, 2017.

  1. ourblok

    ourblok Caaan do!

    Level: Community
    I'm going to use the Juser form plugin so that my team leaders can add members to the site and their group. I'm using the PHP form plugin to insert a row into a table (onAfterProcess) that has: groupid, memberid, approved and permissions - (This is for JomSocial _community_groups_members table).

    I assume that when the form is submitted, it simply autoincrements the userid the for the Joomla _users Table. I need to be able to capture the new user's ID and add it to the insert values as memberid for the _community_groups_members table.

    How, I ask you... HOW?!?!?

    Is there a way I can have an element capture the new userid on submit?

    You're help is valued and appreciated.
     
  2. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    When setting up the juser plugin, you have to specify the element on the form that holds the newly assigned user ID. That element should then contain that ID when your onAfterProcess code runs, so in (say) $formModel->formData['userid'] or $formModel->formDataWithTableName['yourtable___userid'].

    (NOTE that onAfterProcess, entries in $formModel->formData[] have had the tablename___ prefix removed, but there's a copy of the data that still has them, formDataWithTableName)

    -- hugh
     
    ourblok likes this.
  3. ourblok

    ourblok Caaan do!

    Level: Community
    You're solution was so simple I had a hard time accepting it. For other novices like myself, here's exactly what i did.
    • Create a field element called "whatever_you_want". I have Eval set to "No"
    • Using the juser form plugin, be sure that the user id is assigned to "whatever_you_want".
    • Using the php form plugin, for the "Process Script" dropdown, select "End of form submission (onAfterProcess)".
    Here is the code I used:
    Code (Text):
    $newuserid = $formModel->formData['whatever_you_want'];
    return $newuserid;
    Easy-Peezy-Lemon-Squeezy.

    Thanks Hugh for the valuable guidance.
     
  4. ourblok

    ourblok Caaan do!

    Level: Community
    I'm trying to insert this information into a row on a separate table, but it doesn't take.

    $newuserid = $formModel->formData['new_user_id'];
    return $newuserid;

    // Get a db connection.
    $myDb = JFactory::getDbo();

    // Create a new query object.
    $myQuery = $myDb->getQuery(true);

    // Insert columns.
    $columns = array('groupid', 'memberid', 'approved', 'permissions');

    // Insert values.
    $values = array('{general_registration___jomsocial_user_group_id}', '$newuserid', '1', '1');

    // Prepare the insert query.
    $myQuery
    ->insert($myDb->quoteName('xxxxx_community_groups_members'))
    ->columns($myDb->quoteName($columns))
    ->values(implode(',', $myDb->quote($values)));


    // Reset the query using our newly populated query object.
    $myDb->setQuery($myQuery);

    try {
    // Execute the query
    $result = $myDb->execute();
    //use $myDb->query() in Joomla2.5
    }
    catch (Exception $e) {
    // catch any database errors.

    }
    $newid = (int)$myDb->insertid(); //get new record id

    Can you help me out? I think it's just the markup.
     
  5. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    OK, first problem is that on line 2, you do ...

    return $newuserid;

    ... which means the script stops there, and returns that value to whatever called it. So delete that line.

    Then in this line:

    Code (Text):

    // Insert values.
    $values = array('{general_registration___jomsocial_user_group_id}', '$newuserid', '1', '1');
     
    ... you have $newuserid in single quotes. In PHP, that prevent variables from being evaluated, see the first answer here:

    https://stackoverflow.com/questions...ingle-quoted-and-double-quoted-strings-in-php

    ... so that would literally write $newuserid to the table, not the value of the PHP variable.

    And, for reasons unknown, $db->quote() doesn't work like $db->quoteName(). You can't do $db->quote($arrayOfValues) the same way you can do $db->quoteName($arrayOfNames). You have to explcitily quote the values in the array, then just implode it.

    Code (Text):

    // Insert values.
    $values = array(
        $myDb->quote('{general_registration___jomsocial_user_group_id}'),
        $myDb->quote($newuserid),
        $myDb->quote('1'),
        $myDb->quote('1')
    );
     
    Code (Text):

    ->values(implode(',',$values));
     
    Note that Fabrik placeholders are different to PHP variables. We replace those before handing your code to PHP to execute, so what PHP sees is 'whatever you variable was'.

    -- hugh
     
  6. ourblok

    ourblok Caaan do!

    Level: Community
    I appreciate it Hugh, but this still isn't inserting the row.
    Code (Text):

    <?php

    $newuserid = $formModel->formData['new_user_id'];

    // Get a db connection.
    $myDb = JFactory::getDbo();

    // Create a new query object.
    $myQuery = $myDb->getQuery(true);

    // Insert columns.
    $columns = array('groupid', 'memberid', 'approved', 'permissions');

    // Insert values.
    $values = array(
      $myDb->quote('{general_registration___jomsocial_user_group_id}'),
        $myDb->quote($newuserid),
        $myDb->quote('1'),
        $myDb->quote('1')
        );

    // Prepare the insert query.
    $myQuery
        ->insert($myDb->quoteName('xxxxx_community_groups_members'))
        ->columns($myDb->quoteName($columns))
        ->values(implode(',',$values));

    // Reset the query using our newly populated query object.
    $myDb->setQuery($myQuery);

    try {
    // Execute the query
      $result = $myDb->execute();
    //use $myDb->query() in Joomla2.5
    }
    catch (Exception $e) {
    // catch any database errors.

    }
    $newid = (int)$myDb->insertid(); //get new record id


    ?>
     
  7. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    OK, so now it's time to debug.

    Put ...

    var_dump((string)$myQuery);exit;

    ... just before the execute. See what it shows.

    Depending what element type new_user_id is, you may find that it's an array.

    -- hugh
     
  8. ourblok

    ourblok Caaan do!

    Level: Community
    Sorry for the belated here. I took an unearned vacation. I'm back now. I put the php form element like so:

    Code (Text):

    <?php

    $newuserid = $formModel->formData['new_user_id'];

    // Get a db connection.
    $myDb = JFactory::getDbo();

    // Create a new query object.
    $myQuery = $myDb->getQuery(true);

    // Insert columns.
    $columns = array('groupid', 'memberid', 'approved', 'permissions');

    // Insert values.
    $values = array(
      $myDb->quote('{general_registration___jomsocial_user_group_id}'),
        $myDb->quote($newuserid),
        $myDb->quote('1'),
        $myDb->quote('1')
        );

    // Prepare the insert query.
    $myQuery
        ->insert($myDb->quoteName('xxxxx_community_groups_members'))
        ->columns($myDb->quoteName($columns))
        ->values(implode(',',$values));

    // Reset the query using our newly populated query object.
    $myDb->setQuery($myQuery);
    var_dump((string)$myQuery);exit;
    try {
    // Execute the query
      $result = $myDb->execute();
    //use $myDb->query() in Joomla2.5
    }
    catch (Exception $e) {
    // catch any database errors.

    }
    $newid = (int)$myDb->insertid(); //get new record id


    ?>
     
    Note: it does capture the new user id and insert it into the table, but it doesn't insert the row into the jomsocial user table (xxxxx_community_groups_members).

    Here is what I assume the relevant part of the debugger spits out when viewing the form:

    Code (Text):

                                [element_ro] =>
                                [value] =>
                                [element_raw] =>
                                [dataEmpty] => 1
                                [labels] => 0
                                [dlabels] => 0
                                [tipAbove] =>
                                [tipBelow] =>
                                [tipSide] =>
                                [offset] => 0
                                [span] => span12
                                [column] =>  style="clear:both;width:100%;"
                            )

                        [new_user_id] => stdClass Object
                            (
                                [startRow] => 1
                                [endRow] => 1
                                [error] =>
                                [plugin] => field
                                [hidden] => 1
                                [id] => general_registration___new_user_id
                                [className] => fb_el_general_registration___new_user_id
                                [containerClass] => fabrikElementContainer plg-field fb_el_general_registration___new_user_id fabrikHide
                                [element] =>
    <input
        type="hidden" id="general_registration___new_user_id" name="general_registration___new_user_id" class="input-medium form-control  hidden fabrikinput inputbox text" value=""  />

                                [label_raw] => New User ID
                                [label] =>     New User ID
                                [errorTag] => <span class="fabrikErrorMessage">


    </span>

                                [element_ro] => <!--  -->
                                [value] =>
                                [element_raw] =>
                                [dataEmpty] =>
                                [labels] => 0
                                [dlabels] => 0
                                [tipAbove] =>
                                [tipBelow] =>
                                [tipSide] =>
                                [offset] => 0
                                [span] => span12
                                [column] =>  style="clear:both;width:100%;"
                            )

                    )

                [subgroups] => Array
                    (
                    )

                [startHidden] =>
                [repeatIntro] =>
                [class] => fabrikGroup form-horizontal
                [newGroup] =>
            )

    )
     
     
  9. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    If it's showing the usual page with Fabrik debug, then your code isn't running. It should show an otherwise blank page with just your custom query on it.

    Is this code in a file, or in the PHP box?

    If it's in the PHP box, get rid of the enclosing <?php and ?> tags.

    -- hugh
     
  10. ourblok

    ourblok Caaan do!

    Level: Community
    I put this without the php tags into the php form plugin (onAfterProcess). I tried it two ways.
    Code (Text):

    // Get a db connection.
    $myDb = JFactory::getDbo();

    // Create a new query object.
    $myQuery = $myDb->getQuery(true);


    $newid = (int)$myDb->insertid(); //get new record id

    $newuserid = $formModel->formData['new_user_id'];
    return $newuserid;

    // Insert columns.
    $columns = array('groupid', 'memberid', 'approved', 'permissions');

    // Insert values.
    $values = array('{general_registration___jomsocial_user_group_id_raw}', '\''.$newuserid.'\'', '1', '1');

    // Prepare the insert query.
    $myQuery
        ->insert($myDb->quoteName('xxxxxx_community_groups_members'))
        ->columns($myDb->quoteName($columns))
        ->values(implode(',', $myDb->quote($values)));

    // Reset the query using our newly populated query object.
     $myDb->setQuery($myQuery);
    try {
    // Execute the query
    var_dump((string)$myQuery);exit;
      $result = $myDb->execute();
    //use $myDb->query() in Joomla2.5
    }
    catch (Exception $e) {
    // catch any database errors.

    }

     
    and like this

    Code (Text):

    // Get a db connection.
    $myDb = JFactory::getDbo();

    // Create a new query object.
    $myQuery = $myDb->getQuery(true);


    $newid = (int)$myDb->insertid(); //get new record id

    $newuserid = $formModel->formData['new_user_id'];
    return $newuserid;

    // Insert columns.
    $columns = array('groupid', 'memberid', 'approved', 'permissions');

    // Insert values.
    $values = array('{general_registration___jomsocial_user_group_id_raw}', '\''.$newuserid.'\'', '1', '1');

    // Prepare the insert query.
    $myQuery
        ->insert($myDb->quoteName('xxxxxx_community_groups_members'))
        ->columns($myDb->quoteName($columns))
        ->values(implode(',', $myDb->quote($values)));

    // Reset the query using our newly populated query object.
     $myDb->setQuery($myQuery);
    var_dump((string)$myQuery);exit;
    try {
    // Execute the query
      $result = $myDb->execute();
    //use $myDb->query() in Joomla2.5
    }
    catch (Exception $e) {
    // catch any database errors.

    }

     
    Should I be placing this or the vardump snippet you gave me somewhere else?
     
  11. ourblok

    ourblok Caaan do!

    Level: Community
    Does it matter that I'm doing all of this on the backend?
     
  12. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    Erm ... that code is totally different to what I gave you.

    I gave you:

    Code (Text):

    $newuserid = $formModel->formData['new_user_id'];
     
    ... you have ...

    Code (Text):

    $newid = (int)$myDb->insertid(); //get new record id

    $newuserid = $formModel->formData['new_user_id'];
    return $newuserid;
     
    ... which is a) getting a meaningless insertId(), and b) returning $newuserid. In other words, your code stops there, and returns that value to the calling code. Nothing else gets executed.

    -- hugh
     
  13. ourblok

    ourblok Caaan do!

    Level: Community
    I'm a dumbass. :rolleyes:. I owe you a drink if i ever see you.

    Appreciate your patience here. After making the adjustment and using the vardump, this is what i get:

    Code (Text):

    C:\wamp64\www\harvey-app\plugins\fabrik_form\php\php.php(485) : eval()'d code:27:string '
    INSERT INTO `xxxxxxx_community_groups_members`
    (`groupid`,`memberid`,`approved`,`permissions`) VALUES
    ('','\'1406\'','1','1')' (length=128)

    Sooo
    there's progress because now it's inserting a new row, into xxxxxx_community_groups_members, but the values are incorrect. This is what is actually getting entered into phpmyadmin

    Code (Text):

    groupid        memberid    approved    permissions
    0            0               1                   1
     
     
  14. ourblok

    ourblok Caaan do!

    Level: Community
    I guess the backslashes are messing up my operation?
     
  15. ourblok

    ourblok Caaan do!

    Level: Community
    I tried using {general_registration___new_user_id_raw} and I still get the same result
     
  16. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    Again, look at the code I gave you ...

    Code (Text):

    // Insert values.
    $values = array(
      $myDb->quote('{general_registration___jomsocial_user_group_id}'),
        $myDb->quote($newuserid),
        $myDb->quote('1'),
        $myDb->quote('1')
        );

    // Prepare the insert query.
    $myQuery
        ->insert($myDb->quoteName('xxxxx_community_groups_members'))
        ->columns($myDb->quoteName($columns))
        ->values(implode(',',$values));
     
    ... vs what you have:

    Code (Text):

    // Insert values.
    $values = array('{general_registration___jomsocial_user_group_id_raw}', '\''.$newuserid.'\'', '1', '1');

    // Prepare the insert query.
    $myQuery
        ->insert($myDb->quoteName('xxxxxx_community_groups_members'))
        ->columns($myDb->quoteName($columns))
        ->values(implode(',', $myDb->quote($values)));
     
    -- hugh
     
  17. ourblok

    ourblok Caaan do!

    Level: Community
    You need a raise, hugh. Thanks for the support here. All seems to be working.
     
    cheesegrits likes this.

Share This Page