2013-12-19 31 views
0

我是Cakephp的新手,並且遇到以下關聯問題。我所追求的是在表中列出零件以及與其相關的源供應商。因此,在查看/ index.ctp我目前得到的表是CakePhP協會不按預期方式顯示

ID Source  Part # 
1  1  Part00001 

但我想聯想到顯示

ID Source  Part # 
1  Vendor 1 Part00001 

其中Source.name從mstr_sources db表,不mstr_parts表。 以下是所有相關的代碼

class MstrPart extends AppModel { 
    public $name = 'MstrPart'; 
    public $belongsTo = 'MstrSource'; 
} 

class MstrPartsController extends AppController { 
    public function index() { 
     $this->set('parts', $this->paginate()); 
    } 
} 

// <!-- File: /app/View/Users/index.ctp --> 
<?php foreach ($parts as $part): ?> 
<tr> 
    <td><?php echo $part['MstrPart']['id']; ?></td> 
    <td><?php echo $part['MstrPart']['mstr_source_id']['name']; ?></td> 
    //Also tried the following, still fails 
    //<td><?php echo $part['MstrPart']['MstrSource']['name']; ?></td> 
    <td><?php echo $part['MstrPart']['name']; ?></td> 
</tr> 

// Output of the error message context. 
$viewFile = 'C:\Dev\UniServerZ\www\campman\app\View\MstrParts\index.ctp' 
$dataForView = array(
    'parts' => array(
     (int) 0 => array(
      'MstrPart' => array(
       [maximum depth reached] 
      ), 
      'MstrSource' => array(
       [maximum depth reached] 
      ) 
     ) 
    ) 
) 
$parts = array(
    (int) 0 => array(
     'MstrPart' => array(
     'id' => '1', 
     'mstr_source_id' => '1', 
     'created' => '2013-12-18 16:10:35', 
     'modified' => '2013-12-18 16:10:35', 
     'name' => 'Part00001', 
     'description' => 'Just some basic text, not the real thing.' 
     ), 
     'MstrSource' => array(
      'id' => '1', 
      'created' => '2013-12-19 12:23:22', 
      'modified' => '2013-12-19 12:23:22', 
      'name' => 'Vendor 1' 
     ) 
    ) 
) 
$part = array(
    'MstrPart' => array(
     'id' => '1', 
     'mstr_source_id' => '1', 
     'created' => '2013-12-18 16:10:35', 
     'modified' => '2013-12-18 16:10:35', 
     'name' => 'Part00001', 
     'description' => 'Just some basic text, not the real thing.' 
    ), 
    'MstrSource' => array(
     'id' => '1', 
     'created' => '2013-12-19 12:23:22', 
     'modified' => '2013-12-19 12:23:22', 
     'name' => 'Vendor 1' 
    ) 
) 
include - APP\View\MstrParts\index.ctp, line 35 
View::_evaluate() - CORE\Cake\View\View.php, line 929 
View::_render() - CORE\Cake\View\View.php, line 891 
View::render() - CORE\Cake\View\View.php, line 460 
Controller::render() - CORE\Cake\Controller\Controller.php, line 952 
Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 192 
Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 160 
[main] - APP\webroot\index.php, line 108 

片斷是否有,我失去了一些東西明顯?

回答

2

我想你只需要改變這一行

<td><?php echo $part['MstrPart']['mstr_source_id']['name']; ?></td> 

對此

<td><?php echo $part['MstrSource']['name']; ?></td> 

編輯:我會解釋了一下。 在cakephp中,所有連接的模型(通過Model->find中的「連接」標記,可包含的行爲或通過在模型文件中建立的默認綁定進行連接)都通過模型名稱的鍵包含在結果數組中。 *這是他們的型號名稱,而不是表格名稱。有關更多信息,請參閱cakephp naming conventions

既然你想從mstr_source,而不是mstr_part列,要引用MstrSource字典中的數據,而不是MstrPart

+0

謝謝,這是該修補程序。那是我可能忽視的任何地方的文檔嗎? –

+0

是的,你可以在這個頁面上閱讀一點點。 http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-first 您正在使用find('all'),但是數據的結構是最好的我鏈接到的部分。開始使用會讓人感到困惑,但是經過一些試驗和錯誤之後,你就會選擇它。 – isick