0

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

4

1 回答 1

0

关于如何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
                )
        )
    )
)
于 2013-02-25T04:02:14.947 回答