2017-06-22 74 views
0

我顯示我的用戶在一個表中,但我是COMPANY_IDCakePHP的:與其他表的數據表顯示數據

用戶檢索的公司名稱和城市:

id lastname firstname company_id role 
1 doe   john  2   author 
2 deo   jenne  2   admin 
3 deus  Joe   1   admin 
4 Stewart  Rob   1   author 

公司:

id name    city 
1 New-York Company New-York 
2 Paris Company  Paris 

用戶顯示:

  <?php 
       foreach($users as $user):?> 
       <tr> 
        <td><?= $user->lastname?></td> 
        <td><?= $user->firstname?></td> 
        <td><?= $user->companies->name ?></td> 
        <td><?= $user->companies->city?></td> 
        <td><?= $user->role?></td> 
       </tr> 
       <?php endforeach;?>  

usersTable.php:

<?php 
namespace App\Model\Table; 

use Cake\ORM\Table; 
use Cake\Validation\Validator; 

class UsersTable extends Table 
{ 
    public function initialize(array $config){ 
     $this->belongsToMany('Companies'); 
     $this->belongsTo('Companies', [ 
     'foreignKey' => 'company_id', 
    ]); 
} 

}  

?> 
在我的公司顯示

所以,我想找回管理員和作者的名字,像這樣:

   <?php 
       foreach($companies as $company):?> 
       <tr> 
        <td><?= $company->name?></td> 
        <td><?= $company->city?></td> 
        <td><?= $company->users-> *adminlastname* ?></td> 
        <td><?= $company->users-> *adminfirstname* ?></td> 
        <td><?= $company->users-> *authorlastname* ?></td> 
        <td><?= $company->users-> *authorfirstname* ?></td> 
       </tr> 
       <?php endforeach;?>  

我試圖讓公司ID的引用COMPANY_ID在用戶對於初學者走上正軌,沒有成功(試圖獲得非對象的財產)

此外,如果我有幾個作者和管理員,它會工作嗎?不太可能每個公司的幾個管理員,但可能發生,幾位作者是definetely可能

companiesTable:

<?php 
namespace App\Model\Table; 

use Cake\ORM\Table; 
use Cake\Validation\Validator; 

class companiesTable extends Table 
{ 

public function initialize(array $config){ 
    //$this->addBehavior('Timestamp'); 
    $this->hasMany('Users'); 
    $this->hasMany('companies', [ 
     'foreignKey' => 'identite', 
    ]); 
    $this->hasMany('Admins', [ 
'className' => 'Users', 
'conditions' => [ 
    'Admins.role' => 'admin' 
    ] 
]); 

$this->hasMany('Authors', [ 
    'className' => 'Users', 
    'conditions' => [ 
     'Admins.role' => 'author' 
    ] 
]); 


} 

public function isOwnedBy($companyId, $userId){ 
    $company = $this 
     ->find('all',['conditions' => ['companies.id'=>$companyId]]) 
     ->matching('Users', function ($q) use ($userId) { 
      return $q->where(['Users.id' => $userId]); 
     }) 
     ->first(); 
    if ($company) return true; 
    return false; 


} 




} 

編輯:增加公司/ index.ctp,還太新,我沒有看到在所有什麼是錯的...:

<div class="row"> 
    <div class="medium-4 small-12 columns"> 
     <div style="padding: 22px;"> 
      <h2>companies</h2> 

     </div> 

    <div id="customAdd" style="clear:both;padding: 22px;"> 
      <a href="/companies/add" class="button secondary right" data-reveal-id="Modal" data-reveal-ajax="true">add</a>   
<div style="clear:both;height:0px;"></div> 
    </div> 



     <div id="customSearch" style="clear: both;padding: 22px"> 
      <p style="font-size:36px; display: inline-block;">search for a company</p><input type="text" id="searchBox"> 
     </div> 


    </div> 
    <div class="medium-8 small-12 columns"> 
     <div> 

      <table id="companies" class="cell-border dataTable no-footer"> 
       <thead> 
        <tr> 
         <th>name</th> 
         <th>city</th> 
         <th>last name</th> 
         <th>first name</th> 

        </tr> 
       </thead> 
       <tbody> 
        <?php foreach ($company->users as $user):?> 

        <tr class='clickable-row' data-href="/companies/edit/<?= $company->id?>"> 

        <td><?= $company->name?></td> 
        <td><?= $company->city?></td> 
        <td><?= $company->users[0]->lastname?></td> 
        <td><?= $company->users[0]->firstname?></td> 

        </tr> 
        <?php endforeach;?> 
       </tbody> 
      </table> 

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

我得到這些錯誤:

Notice (8): Undefined variable: company [APP/Template/Companies/index.ctp, line 36] 
Notice (8): Trying to get property of non-object [APP/Template/Companies/index.ctp, line 36] 
Warning (2): Invalid argument supplied for foreach() [APP/Template/Companies/index.ctp, line 36] 

WHI CH對應於此:

<?php foreach ($company->users as $user):?> 

CompaniesController:

<?php 

namespace App\Controller; 

use Cake\ORM\TableRegistry; 


class CompaniesController extends AppController{ 

    public function index(){ 
     $companies = $this->Companies 
      ->find('all',['order' => ['Companies.name' => 'ASC']]); 
     $this->set(compact('companies')); 

     $this->loadModel('Users'); 
     $users = $this->Users->find('all') 
      ->contain(['companies']); 
     $this->set(compact('users')); 




    } 

    public function view($id = null){ 
     $company = $this->Companies->get($id); 
     $this->set(compact('company')); 
    } 

    public function add(){ 
     $company = $this->Companies->newEntity(); 
     if ($this->request->is('post')) { 

      if ($this->Companies->save($company)) { 

       $this->Flash->success(__('Your company has been saved.')); 

       return $this->redirect(['action' => 'index']); 
      } 
      $this->Flash->error(__('Unable to add your company.')); 
     } 
     $this->set('company', $company); 

     $this->set(compact('companies')); 
    } 

    public function edit($id = null){ 
     $company = $this->Companies->get($id); 
     if ($this->request->is(['post', 'put'])) { 
      $company = $this->Companies->patchEntity($company, $this->request->data,['associated' => ['Users']]); 


      if ($this->Companies->save($company)) { 



       $this->Flash->success(__('Your company has been updated.')); 
       return $this->redirect(['action' => 'index']); 
      } 

      $this->Flash->error(__('Unable to update your company.')); 
     } 


     $this->set('company', $company); 

     $this->set(compact('companies')); 

    } 

    public function delete($id){ 
     $this->request->allowMethod(['post', 'delete']); 
     $company = $this->Companies->get($id); 
     if ($this->Companies->delete($company)) { 
      $this->Flash->success(__('The company with id: {0} has been deleted.', h($id))); 
      return $this->redirect(['action' => 'index']); 
     } 
    } 

    public function isAuthorized($user){ 
     if ($this->request->action === 'logout') { 
      return true; 
     } 
     if ($this->request->action === 'settings') { 
      return true; 
     } 
      return parent::isAuthorized($user); 
    } 
} 

由於事先第一

回答

2

第一件事,你不能使用相同的名稱定義多個關聯。考慮到您有一個company_id字段,那麼從用戶的角度來看,這是一個belongsTo關聯。

鑑於這些先決條件,它自然是從公司的角度來看hasMany協會。我假設你已經在CompaniesTable課程中設置過,如果不是,那麼這就是你首先需要做的。然後

users屬性將是User entitiy對象的數組,所以需要相應地訪問它,要麼是通過直接的索引:

$company->users[0]->firstname 

或通過遍歷它:

foreach ($company->users as $user) { 
    // $user->firstname 
} 

如果您需要處理由其角色分隔的用戶,則可以相應地對其進行過濾:

$admins = collection($company->users)->filter(function ($user) { 
    return $user->role === 'admin'; 
}); 

也許更好,但創建單獨的協會使用條件:

$this->hasMany('Admins', [ 
    'className' => 'Users', 
    'conditions' => [ 
     'Admins.role' => 'admin' 
    ] 
]); 

$this->hasMany('Authors', [ 
    'className' => 'Users', 
    'conditions' => [ 
     'Admins.role' => 'author' 
    ] 
]); 

這種方法你可以包含並分別訪問不同類型的用戶:

$company->admins 
$company->authors 

也見

+0

Mmmh我得到使用'的foreach($公司 - >用戶以$用戶)提供的foreach無效參數{' –

+0

然後'users'是不是你認爲它是,最像是因爲你沒有包含該協會。 @ Nolan.K – ndm

+0

謝謝,添加了我的公司表。試圖找出用戶是什麼 –

相關問題