如何使用HTML->链接链接到另一个表

时间:2016-07-28 19:49:56

标签: php cakephp cakephp-3.0

我正在尝试设置链接以从users表中查看用户。我希望它出现在文章模板的索引文件中。

我尝试过使用:

$this->Users->Html->link($current->username, ['action' => 'view', $current->id]) ?>

$this->$userstable->Html->link($current->username, ['action' => 'view' $current->id]) ?>

这是索引文件:

<div class="col-lg-8">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 style="float:left;">Users</h4>
                <a href="http://localhost:8765/users/add"><Button class="btn btn-primary add-article">Add User</Button></a>
            </div>
            <table>
                <tr>
                    <th>Id</th>
                    <th>Userame</th>
                    <th>Role</th>
                    <th>Joined</th>


                </tr>

            <!-- Here's where we loop through our $articles query object, printing out article info -->

                <?php foreach ($userstable as $current): ?>
                <tr>
                    <td><?= $current->id ?></td>
                    <td>
                        <?= $this->Html->link($current->username, ['action' => 'view', $current->id]) ?>
                    </td>
                    <td>
                        <?= $current->role?>
                    </td>
                    <td>
                        <?= $current->created->format(DATE_RFC850) ?>
                    </td>
                    <td>
                        <?= $this->Form->postLink(
                            'Delete',
                            ['action' => 'delete', $current->id],
                            ['confirm' => 'Are you sure?'])
                        ?>
                        <?= $this->Html->link('Edit', ['action' => 'edit', $current->id]) ?>

                    </td>


                </tr>
                <?php endforeach; ?>

            </table>
        </div>
    </div>

这是控制器:

public function index()
{


    $this->paginate = [
    'contain'=>['Categories']
    ];
    $articles = $this->paginate($this->Articles);
    $this->set('articles', $this->Articles->find('all'));
    $this->set(compact('articles'));
    $this->set('_serialize', ['articles']);

    $article_count = $this->Articles->find('all')->count();
    $this->set('article_count',$article_count );

    /*$author_count = $this->Articles->find('all')
        ->distinct(['Articles.user_id'])
        ->count('user_id');
    $this->set('author_count',$author_count );*/

    $this->loadModel('Categories');
    $categoryCount = $this->Categories->find('all')->count();
    $this->set('categoryCount',$categoryCount );

    $this->loadModel('Users');
    $userstable = $this->Users->find('all');
    $this->set('userstable',$userstable );

    $usersCount = $this->Users->find('all')->count();
    $this->set('usersCount',$usersCount );

    $authorCount= $this->Users->find('all')->where(['role' => 'author'])->count();
    $this->set('authorCount',$authorCount );

    $adminCount= $this->Users->find('all')->where(['role' => 'admin'])->count();
    $this->set('adminCount',$adminCount );

    $subCount= $this->Users->find('all')->where(['role' => 'admin'])->count();
    $this->set('subCount',$subCount );

    $this->loadModel('Views');
    $views = $this->Views;

    $query = $views->find('all')->limit(7)->order(['date' => 'DESC']);;
    $results = $query->all();
    $data = $results->toArray();
    $results = $query->toArray();

    $result1 = $results[6]['amount'];
    $this->set('result1',$result1 );

    $result2 = $results[5]['amount'];
    $this->set('result2',$result2 );

    $result3 = $results[4]['amount'];
    $this->set('result3',$result3 );

    $result4 = $results[3]['amount'];
    $this->set('result4',$result4 );

    $result5 = $results[2]['amount'];
    $this->set('result5',$result5 );

    $result6 = $results[1]['amount'];
    $this->set('result6',$result6 );

    $result7 = $results[0]['amount'];
    $this->set('result7',$result7 );

    $date1 = $results[6]['date'];
    $this->set('date1',$date1 );

    $date2 = $results[5]['date'];
    $this->set('date2',$date2 );

    $date3 = $results[4]['date'];
    $this->set('date3',$date3 );

    $date4 = $results[3]['date'];
    $this->set('date4',$date4 );

    $date5 = $results[2]['date'];
    $this->set('date5',$date5 );

    $date6 = $results[1]['date'];
    $this->set('date6',$date6 );

    $date7 = $results[0]['date'];
    $this->set('date7',$date7 );




}

您可以给予任何帮助

1 个答案:

答案 0 :(得分:0)

简单地说:

$this->Html->link($current->username, [
    'controller' => 'Users',
    'action' => 'view', 
    $current->id
]);

http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-links