2016-07-28 115 views
0

我想設置一個鏈接來查看用戶從用戶表。我希望它出現在文章模板的索引文件中。如何使用HTML->鏈接鏈接到另一個表

我已經嘗試使用:

$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); 




} 

你可以給任何幫助,將不勝感激

回答

相關問題