【问题标题】:CakePHP 3 - Model - Accessing data from third layer of arrayCakePHP 3 - 模型 - 从数组的第三层访问数据
【发布时间】:2017-03-01 08:16:32
【问题描述】:

CakePHP 3

我的模型

用户有很多徽章

标题有很多徽章

徽章属于标题

徽章属于用户

在我的用户视图中,我想访问 Titles 的值,但我不知道如何访问。

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

【问题讨论】:

    标签: cakephp-3.0


    【解决方案1】:

    在您的contain 中,您可以使用'Model.Model' 获取更多相关数据,如下所示:

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

    【讨论】:

    • 有效!精彩的!现在,如何获取视图中的数据?
    • 这是我的用户视图代码。 badges as $badges): ?>
    • Html->link($badges->name, ['controller' => 'Badges', ' action' => 'view', $badges->id]);?>
  • 你想清楚了吗?您可以在视图中调试$user,然后使用foreach 或者我需要查看您的调试以获得进一步的帮助。
  • 是是是!。非常感谢!
  • 很高兴为您提供帮助 :)
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签