cakephp模型关联(视图) [英] cakephp model association (view)

查看:152
本文介绍了cakephp模型关联(视图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In cakephp I have codesets and codesetitems. codesetitems belong to codesets so in my codesetitems I have belongsTo = 'Codeset'. But in my view I don't seem to be able to call $codeset['Codesetitem']['id']. It says undefined index Codesetitem. I already checked out the cake documentation. One codeset can have many codesetitems.

推荐答案

关于 cakephp 并输出结果数组。

为了获得关联结果,必须在每个模型中定义如下。

In order to get associated result you must define it in each model as below.

CodesetItem模型

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

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

代码集模型

Codeset Model

<?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' => ''
        )
    );
}
?>

代码集控制器

Codeset Controller

<?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方法它将如下所示。

but when you use find('all') in find method it will out as below.

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
                )
        )
    )
)

这篇关于cakephp模型关联(视图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆