0

Cakephp には、コードセットとコードセット項目があります。codesetitems はコードセットに属しているため、私の codesetitems には belongsTo = 'Codeset' があります。しかし、私の見解では、$codeset['Codesetitem']['id'] を呼び出すことができないようです。未定義のインデックス Codesetitem と表示されます。私はすでにケーキのドキュメントをチェックアウトしました。1 つのコードセットに多くのコードセット項目を含めることができます。

4

1 に答える 1

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('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 に答える