Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Ormauth get_groups() not working as expected. Docs example fails.
  • I am trying to retrieve the list of groups a member is in as per the get_groups() example on http://fuelphp.com/docs/packages/auth/ormauth/usage.html

    When I use this example however, I get the error:

    Fuel\Core\PhpErrorException [ Runtime Recoverable error ]:
    Object of class Auth\Model\Auth_Group could not be converted to string

    The line indicates is the line from the example:

    echo 'Driver: ',$info[0],' with group ID ',$info[1],'<br />';

    If I echo out just $into[0[ I get ""Ormgroup".  If I print_r() out $info[1] I get an Auth\Model\Auth_Group Object.

    Could someone please point me in the direction of what I am doing wrong? This seems like it should be simple enough.  The user is logged in and I am able to obtain the user ID so Auth itself is working...








  • HarroHarro
    Accepted Answer
    That looks like a copy/paste from SimpleAuth, and is indeed wrong.

    Ormauth returns Orm models, not strings.
  • That explains things then... The documentation on the site for ORMauth is wrong then unfortunately :(.

    Do you know if there is any correct documentation anywhere on how to use the ORM model returned by ORMauth? I'm afraid I'm not following how to properly interface with this now. Looking through the ORM docs I'm not sure how to adapt it to this type of use.
  • ORM objects are simple (from the outside), every column in the table the object is linked to will be a property in the object.

    So to get the group name, you do access $object->group.
  • Changing $info[1] to $info[1]->id in the OrmAuth get_groups() docs fixes the problem.
    Can someone update this so the docs don't misinform others? Thanks! :)

    echo 'Driver: ',$info[0],' with group ID ',$info[1]->id,'<br />';

  • As you sure that is it?

    According to the code, get_groups returns array('Ormgroup, $user->group)).

    $user->group is a one-to-many relation, which means it's an array of zero or more group objects. So $info[1] is not an object, it's an array of objects.

    Reason for this is that contrary to Simpleauth, in Ormauth a user can be member of multiple groups.

  • I'm seeing it returning an array of array(array('Ormgroup', $this->user->group)); -- Note an array within an array. This is in v1.7 in the ormauth.php. $this->user->group is indeed a single object for me, not a one-to-many relation.

    This gets the value from the group_id field in the users table. I'm not sure how it goes about returning multiple groups-- perhaps that is with the group() method?

    Anyways, the code I'm successfully using is:
    $groups = Auth::get_groups();
    foreach( $groups as $group ) {
    echo 'Group ID is ',$group[1]->id,' and Group Name is ',$group[1]->name;
    }

    A modified example from the current SimpleAuth docs can be found here:

    Maybe things have changed in the latest dev versions...
  • HarroHarro
    Accepted Answer
    You're right, I should have checked the model code too.

    It's defined as Group -> hasmany -> User, User -> belongsto -> Group.

    So there is only one group possible per user (which is probably done for compatibility with Simpleauth). It returns a nested array because you can have multiple group drivers active, and each will return an array with the driver name and the group.

    I'll change the docs.

Howdy, Stranger!

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

In this Discussion