cakephp模型关联(视图)

编程入门 行业动态 更新时间:2024-10-28 08:23:31
本文介绍了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模型关联(视图)

本文发布于:2023-10-28 22:38:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537974.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   模型   cakephp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!