cakephp模型协会(查看)

时间:2013-02-24 17:37:01

标签: cakephp

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

1 个答案:

答案 0 :(得分:0)

关于cakephp如何处理和输出结果数组的说明。

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

CodesetItem模型

<?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(&#39; all&#39;)时,它将如下所示。

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
                )
        )
    )
)
相关问题