CakePHP 3 - Model - Accessing data from third layer of array
04:15 01 Mar 2017

CakePHP 3

My Model

  • Users has Many Badges
  • Titles has Many Badges
  • Badges belongs to Titles
  • Badges belongs to Users

In my Users View, I want to access the value of Titles but I don't know how.

Users Controller

public function view($id = null)
{
    $this->viewBuilder()->layout('admin');
    TableRegistry::get('Users');
    $user = $this->Users->get($id, [
        'contain' => ['Badges', 'Projects', 'Reports', 'Rewards']
    ]);
    $this->set(compact('user'));
    $this->set('_serialize', ['user']);
}
    

UsersTable.php

$this->hasMany('Badges', [
    'foreignKey' => 'user_id'
]);

TitleTable.php

$this->belongsTo('Users', [
    'foreignKey' => 'user_id',
    'joinType' => 'INNER'
]);
$this->belongsTo('Titles', [
    'foreignKey' => 'title_id',
    'joinType' => 'INNER'
]);

BadgesTable.php

$this->hasMany('Badges', [
    'foreignKey' => 'title_id'
]);
App\Model\Entity\User Object
(
    [id] => 1
    [firstname] => Jl
    [lastname] => C
    [email] => m
    [username] => admin
    [password] => 
    [profile_pic] => /img/noimage.jpg
    [report_count] => 0
    [role] => admin
    [reputation] => 999999
    [account_status] => Active
    [confirmation] => 1
    [site_name] => C
    [site_email] => mi
    [created] => Cake\I18n\FrozenTime Object
        (
            [time] => 2017-02-27T13:46:51+08:00
            [timezone] => Asia/Manila
            [fixedNowTime] => 
        )

    [modified] => Cake\I18n\FrozenTime Object
        (
            [time] => 2017-03-01T10:37:50+08:00
            [timezone] => Asia/Manila
            [fixedNowTime] => 
        )

    [badges] => Array
        (
            [0] => App\Model\Entity\Badge Object
                (
                    [id] => 1
                    [user_id] => 1
                    [title_id] => 11
                    [created] => Cake\I18n\FrozenTime Object
                        (
                            [time] => 2017-02-28T15:16:08+08:00
                            [timezone] => Asia/Manila
                            [fixedNowTime] => 
                        )

                    [modified] => Cake\I18n\FrozenTime Object
                        (
                            [time] => 2017-02-28T15:16:08+08:00
                            [timezone] => Asia/Manila
                            [fixedNowTime] => 
                        )

                    [[new]] => 
                    [[accessible]] => Array
                        (
                            [*] => 1
                        )

                    [[dirty]] => Array
                        (
                        )

                    [[original]] => Array
                        (
                        )

                    [[virtual]] => Array
                        (
                        )

                    [[errors]] => Array
                        (
                        )

                    [[invalid]] => Array
                        (
                        )

                    [[repository]] => Badges
                )
        )

    [[new]] => 
    [[accessible]] => Array
        (
            [*] => 1
        )

    [[repository]] => Users
)

What I want is to get the data from title table (i.e name)

[badges] => Array
(
    [0] => App\Model\Entity\Badge Object
    (
        [id] => 1
        [user_id] => 1
        [title_id] => 11
        [created] => Cake\I18n\FrozenTime Object
php relationship cakephp-3.0