Results 1 to 4 of 4

Thread: PHP function to add user to usergroup Contao 3.5

  1. #1
    New user
    Join Date
    09-20-15.
    Posts
    19

    Default PHP function to add user to usergroup Contao 3.5

    Hello Community,

    I am trying to make e PHP function that will run when a hook is triggered.

    The function must add a user to a user-group.

    This is what i have so far

    PHP Code:
    $userId $arrData["member_id"];
    $groupId "3";

    $groups $this->Database->prepare('SELECT groups FROM tl_member WHERE id=?')->execute($userId)->next()->groups;

    $groups unserialize($groups);
    array_push($groups$groupId);
    $groups serialize($groups);

    $this->Database->prepare('UPDATE tl_member %s WHERE id=?')->set(array('groups' =&gt$groups))->execute($userId); 
    This will update the user-group,
    But do i need to add an entry to "tl_member_to_group" because there is no entry and when i add someone to a user-group in the backend it will add it also to "tl_member_to_group"
    Last edited by xchs; 01/25/2016 at 00:56. Reason: code formatting

  2. #2
    User Andreas's Avatar
    Join Date
    07-11-09.
    Location
    Mönchengladbach
    Posts
    499

    Default

    Don't know "tl_member_to_group". But handling of members, users and groups is easy if you use the models:
    https://github.com/contao/core/blob/...el.php#L14-L35

    Also interesting:
    https://github.com/contao/core/blob/...ontendUser.php

    Also take a look at the models tutorial in contao docs.

    Please show PHP-code as PHP:
    PHP Code:
    $userId $arrData["member_id"];
    $groupId "3";

    $groups $this->Database->prepare('SELECT groups FROM tl_member WHERE id=?')->execute($userId)->next()->groups;

    #$groups = unserialize($groups);
    $groups deserialize($groups);
    array_push($groups$groupId);
    $groups serialize($groups);

    $this->Database->prepare('UPDATE tl_member %s WHERE id=?')->set(array('groups' => $groups))->execute($userId); 
    Web-Development, Freelancer, Burgtech, XHTML, HTML5, CSS, PHP, Javascript, MooTools, MySQL and more
    Amazon wishlist

  3. #3
    New user
    Join Date
    09-20-15.
    Posts
    19

    Default

    Thanks,

    I have found out that the "tl_member_to_group" is for development only.
    See : https://github.com/friends-of-contao...ssociategroups

    Thanks for the hint of user models and for the deserialize change.

  4. #4
    User Andreas's Avatar
    Join Date
    07-11-09.
    Location
    Mönchengladbach
    Posts
    499

    Default

    unserialize() ist PHP, deserialize() is a modified function by Contao.
    Web-Development, Freelancer, Burgtech, XHTML, HTML5, CSS, PHP, Javascript, MooTools, MySQL and more
    Amazon wishlist

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •