2017-03-01 6 views
0

CakePHPの3CakePHPの3 - モデル - 配列の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 

は、あなたがそうのように、さらに関連するデータを取得するために'Model.Model'を使用することができ、あなたのcontainでタイトルテーブル(すなわち名前)

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

答えて

1

からデータを取得することです:

$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' => 'バッジ' action '=>' view '、$ badges-> id]);?>
  • <?php endforeach; ?> – JohnC

    +0

    あなたはまだそれを理解しましたか?ビューで '$ user'をデバッグしてから、' foreach'を使うか、デバッグを見て、もっと助けてください。 – Aarrbee

    関連する問題