2017-03-01 67 views
0

CakePHP的3CakePHP的3 - 模型 - 從陣列的第三層訪問數據

我的模型

用戶具有許多徽章

標題具有許多徽章

徽章屬於標題

徽章屬於用戶

在我的用戶視圖中,我想訪問標題的值,但我不知道如何。

**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 

我想要的是得到標題表(姓名)

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

回答

1

的數據在你contain,您可以使用'Model.Model'得到像這樣進一步的相關數據:

$user = $this->Users->get($id, [ 
    'contain' => ['Badges.Titles', 'Projects', 'Reports', 'Rewards'] 
]); 
+0

它的工作原理!精彩!現在,我如何在我的視圖中獲取數據? – JohnC

+0

這是我的用戶視圖代碼。 <?php foreach($ user-> badges as $ badges):?>

  • <?php echo $ this-> Html-> link($ badges-> name,['controller'=>'Badges',' action'=>'view',$ badges-> id]);?>
  • <?php endforeach; ?> – JohnC

    +0

    你有沒有想過呢?你可以在視圖中調試'$ user',然後使用'foreach'或者我需要看你的調試以獲得進一步的幫助。 – Aarrbee