如何使用hasMany关系打印数据?

时间:2017-09-02 03:11:29

标签: cakephp cakephp-3.0

UsersTable.php

public function initialize(array $ config)     {

    parent::initialize($config);
    $this->belongsTo('Users');
    $this->hasMany('Subjects');
    $this->table('users');       

    $this->displayField('username');  
    $this->primaryKey('id'); 
}

enter image description here

如何打印包括来自其他表的数据,就像我在question上所做的那样我希望在用户表中显示所有具有成绩的科目

用户

*** view.ctp *****

<div class="actions columns large-2 medium-3">
    <h3><?= __('Actions') ?></h3>
    <ul class="side-nav">
        <li><?= $this->Html->link(__('Edit User'), ['action' => 'edit', $users->id]) ?> </li>
        <li><?= $this->Form->postLink(__('Delete User'), ['action' => 'delete', $user->id], ['confirm' => __('Are you sure you want to delete # {0}?', $user->id)]) ?> </li>
        <li><?= $this->Html->link(__('List Users'), ['action' => 'index']) ?> </li>
        <li><?= $this->Html->link(__('New User'), ['action' => 'add']) ?> </li>
    </ul>
</div>
<div class="users view large-10 medium-9 columns">
<?php   

foreach ($users as $user): ?>
    <div class="row">
        <div class="large-5 columns strings">
            <h6 class="subheader"><?= __('Username') ?></h6>
            <p><?= h($user->username) ?></p>

        </div>
        <div class="large-2 columns numbers end">
            <h6 class="subheader"><?= __('Id') ?></h6>
            <p><?= $this->Number->format($user->id) ?></p>
        </div>
    </div>
 <?php endforeach; ?>
</div>

0 个答案:

没有答案