Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Relations: Belongs To
  • Hi everyone,
    I have this definition in one controller

            $data['users'] = \Model\Auth_User::find('all');
            $data['groups'] = \Model\Auth_Group::find_by_id('name');

    And this on my index view:

                <td><?php echo $user->username; ?></td>
                <td><?php echo "$user->group_id | ".$group->name; ?></td>

    but i can't get the name of the group that user belong to. I understand how to create the element with this relation, but i don't understand how to call the elements from each table.
    I have the table users, with various columns and one is group_id
    And the table groups with id and name.
    I need to read the user data and show group name not group id.
  • HarroHarro
    Accepted Answer
    Check the model to see how the relation is defined.

    \Auth\Model_User defines:

    /**
    * @var array belongs_to relationships
    */
    protected static $_belongs_to = array(
    'group' => array(
    'model_to' => 'Model\\Auth_Group',
    'key_from' => 'group_id',
    'key_to'   => 'id',
    'cascade_delete' => false,
    ),
    );

    Which means the name can be accessed using $user->group->name.

    Note that it will not use the result of your own Auth_Group query, and it will run an additional query for each of the users listed in your view, because you did not query the relation.

    So if you need the related data for each record, do

    $data['users'] = \Model\Auth_User::find('all', array('related' => array('group')));

    or 

    $data['users'] = \Model\Auth_User::query()->related('group')->get();
  • Hey Harro Verton,
    Thank for answer me.
    When i try to use $user->group->name give me this error:

         Fuel\Core\PhpErrorException [ Notice ]:
         Trying to get property of non-object

    but, if i use $user->group['name'], then, do work correctly.

    Have you a idea what is origin of the error.?

    The Model is defined who you say.
  • I don't know. Do you pass data to your view as an array?

    What does 

    var_dump($user->group) show you? It should be an Auth\Model_Group object.
  • Harro,

    Yes, I pass data as array:

            $data['users'] = \Model\Auth_User::find('all', array('related' => array('group','roles')));
            $data['groups'] = \Model\Auth_Group::find_by_id('name');
            $data['roles'] = \Model\Auth_Role::find_by_user_id('id');
            $data['permissions'] = \Model\Auth_Userpermission::find_by_user_id('id');
            $this->template->title = "Users";
            $this->template->content = View::forge('admin/acl/index', $data);

    The var_dump($user->group) function show me that

    object(Auth\Model\Auth_Group)#62 (12) {
    ["_is_new":protected]=>
    bool(false)
    ["_frozen":protected]=>
    bool(false)
    ["_sanitization_enabled":protected]=>
    bool(true)
    ["_data":protected]=>
    array(5) {
    ["id"]=>
    string(1) "2"
    ["name"]=>
    string(9) "Invitados"
    ["user_id"]=>
    string(1) "1"
    ["created_at"]=>
    string(1) "0"
    ["updated_at"]=>
    string(10) "1495716552"
    }
    ["_custom_data":protected]=>
    array(0) {
    }
    ["_original":protected]=>
    array(5) {
    ["id"]=>
    string(1) "2"
    ["name"]=>
    string(9) "Invitados"
    ["user_id"]=>
    string(1) "1"
    ["created_at"]=>
    string(1) "0"
    ["updated_at"]=>
    string(10) "1495716552"
    }
    ["_data_relations":protected]=>
    array(0) {
    }
    ["_original_relations":protected]=>
    array(0) {
    }
    ["_reset_relations":protected]=>
    array(0) {
    }
    ["_disabled_events":protected]=>
    array(0) {
    }
    ["_view":protected]=>
    NULL
    ["_iterable":protected]=>
    array(0) {
    }
    }

  • HarroHarro
    Accepted Answer
    You pass the User structure as objects, so $user->group->name should work fine.

    You probably only have to check in your view that $user->group is an object, which it isn't if the user does not have a group assigned, or has assigned a group that does not exist.

Howdy, Stranger!

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

In this Discussion