【问题标题】:cakephp model association (view)cakephp 模型关联(查看)
【发布时间】:2013-02-24 17:37:00
【问题描述】:

在 cakephp 中,我有代码集和代码集项。 codesetitems 属于 codesets,所以在我的 codesetitems 中,我有 belongsTo = 'Codeset'。但在我看来,我似乎无法调用 $codeset['Codesetitem']['id']。它说未定义的索引代码集。我已经查看了蛋糕文档。一个代码集可以有多个代码集项。

【问题讨论】:

  • 您的 CodesetController 是否传递了一个正确构造的 $codeset 数组,该数组实际上包含 Codesetitem 对象?您可以在控制器中 $this->log($codeset) 然后在 PHP 错误文件中查找输出。如果您使用的是传统网页,我认为 debug() 语句将以 HTML 格式打印输出。
  • 您如何 a) 检索数据,b) 将其发送到视图?

标签: cakephp


【解决方案1】:

解释cakephp如何处理和输出结果数组。

为了获得相关结果,您必须在每个模型中定义它,如下所示。

代码集项目模型

<?php
class CodesetItem extends AppModel
{
    var $name = 'CodesetItem';

    var $belongsTo = array
    (
        'Codeset' => array
        (
            'className' => 'Codeset',
            'foreignKey' => 'codeset_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
}
?>

代码集模型

<?php
class Codeset extends AppModel
{
    var $name = 'Codeset';

    var $hasMany = array
    (
        'CodesetItem' => array
        (
            'className' => 'CodesetItem',
            'foreignKey' => 'codeset_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );
}
?>

代码集控制器

<?php
class CodesetsController extends AppController
{
    var $name = 'Codesets';

    function beforeFilter()
    {
        parent::beforeFilter();
    }

    function index()
    {
        $codesets = $this->Codeset->find('first');
        pr($codesets);
        exit;
    }
}
?>

上面将输出索引为 0 的代码集数组,表示如下

Array
(
    [Codeset] => Array
    (
        [id] => 121
        [name] => Gwoo the Kungwoo
        [created] => 2007-05-01 10:31:01
    )
    [CodesetItem] => Array
    (
        [0] => Array
            (
                [id] => 123
                [codeset_id] => 121
                [title] => On Gwoo the Kungwoo
                [body] => The Kungwooness is not so Gwooish
                [created] => 2006-05-01 10:31:01
            )
        [1] => Array
            (
                [id] => 124
                [codeset_id] => 123
                [title] => More on Gwoo
                [body] => But what of the ‘Nut?
                [created] => 2006-05-01 10:41:01
            )
    )
)

但是当你在 find 方法中使用 find('all') 时,它会如下所示。

Array
(
    [0] => Array
    (
        [Codeset] => Array
        (
            [id] => 121
            [name] => Gwoo the Kungwoo
            [created] => 2007-05-01 10:31:01
        )
        [CodesetItem] => Array
        (
            [0] => Array
                (
                    [id] => 123
                    [codeset_id] => 121
                    [title] => On Gwoo the Kungwoo
                    [body] => The Kungwooness is not so Gwooish
                    [created] => 2006-05-01 10:31:01
                )
            [1] => Array
                (
                    [id] => 124
                    [codeset_id] => 121
                    [title] => More on Gwoo
                    [body] => But what of the ‘Nut?
                    [created] => 2006-05-01 10:41:01
                )
        )
    )
    [1] => Array
    (
        [Codeset] => Array
        (
            [id] => 121
            [name] => Gwoo the Kungwoo
            [created] => 2007-05-01 10:31:01
        )
        [CodesetItem] => Array
        (
            [0] => Array
                (
                    [id] => 123
                    [codeset_id] => 121
                    [title] => On Gwoo the Kungwoo
                    [body] => The Kungwooness is not so Gwooish
                    [created] => 2006-05-01 10:31:01
                )
            [1] => Array
                (
                    [id] => 124
                    [codeset_id] => 121
                    [title] => More on Gwoo
                    [body] => But what of the ‘Nut?
                    [created] => 2006-05-01 10:41:01
                )
        )
    )
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多