CakePhP关联未按预期显示

时间:2013-12-19 20:52:53

标签: cakephp model associations

我是Cakephp的新手,我遇到以下关联问题。我所追求的是在表格中列出零件及其相关的源供应商。所以在View / index.ctp中,我目前得到的表是

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

有什么明显的东西我不见了吗?

1 个答案:

答案 0 :(得分:2)

我认为你只需要改变这一行

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

到此

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

编辑:我会解释一下。 在cakephp中,所有连接的模型(通过Model->find中的'join'标签连接,可包含的行为,或通过模型文件中建立的默认绑定)都通过其模型的键包含在结果数组中名称。 *这是他们的型号名称,而不是表名。有关详情,请参阅cakephp naming conventions

由于您需要mstr_source而非mstr_part的列,因此您希望引用MstrSource词典中的数据,而不是MstrPart