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
片斷是否有,我失去了一些東西明顯?
謝謝,這是該修補程序。那是我可能忽視的任何地方的文檔嗎? –
是的,你可以在這個頁面上閱讀一點點。 http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-first 您正在使用find('all'),但是數據的結構是最好的我鏈接到的部分。開始使用會讓人感到困惑,但是經過一些試驗和錯誤之後,你就會選擇它。 – isick