Cakephp 1.3 HABTM问题! :D [英] Cakephp 1.3 HABTM issue! :D
问题描述
嘿,我有cakephp这个问题,基本上我有一个Has和属于许多(HABTM)模型关系。
我的模型是Categroy和Project
带来所有项目数据都很好,它出现 [0] ['Project']
, [1] ['Project']
... etc
但是当我使用关系并拉出项目时,类别控制器我得到这些tpye的结果[0](所有项目数据在[0],而不是[0] ['项目']),[1](项目数据和相关的模型信息)这真的搞乱我的代码因为我使用一个元素视图文件来渲染我的项目有什么办法返回[0] ['项目']为项目控制器和类别控制器?感谢Chris
如果我的示例不清楚,很抱歉
我有项目和类别
当我从我的项目模型中获取项目控制器的项目列表时,我得到的结果是这种格式
[0] ['Project'] = array(data ...);
[1] ['Project'] = array(data ...);
[2] ['Project'] = array(data ...);
这是数据是如何拉的,这对我很好,但是当我拉每个cetegory页面项目在类别控制器中的HABTM关系从类别模型这是我的数据返回
['Project'] [0] = array(data ...);
['Project'] [1] = array(data ...);
['Project'] [2] = array(data ...);
正如你可以看到的一点紧张,因为我想保留1元素视图文件显示我的项目,到目前为止我的视图文件打印数据像这样
<?php print $ project ['Project'] [ '标题']; ?> // data is returned [x] ['Project']
<?php print $ project ['Feature'] ['title']; ?>
与HABTM关系返回数据的方式我需要这样做
<?php print $ project ['title']; ?> //因为数据被返回['Project'] [x]
<?php print $ project ['Feature'] ['title']; ?>
任何人都可以帮助这个?感谢
这也让我很沮丧。我喜欢有一组元素,可以用于渲染主要查找结果以及相关的查找结果。
这是我目前处理的方式
当调用find on,比如说一个Project模型并且想渲染相关的Task列表时,我运行Task
echo $ this-> element(' tasks / index',array(
'data'=> make_primary('Task',$ data ['Task'])
)
我的'make_primary'函数如下:
function make_primary($ alias,$ data){
$ d = array();
foreach($ data as $ item){
$ related = array();
foreach($ item as $ key => $ val){
if(!is_numeric($ key)&& is_array($ val)){
$ related [$ key ] = $ val;
unset($ item [$ key]);
}
}
$ d [] = array_merge(array($ alias => $ item),$ related);
}
return $ d;
}
这会返回一个新的数组,就像它是primary查找查询。
Hey, im having this issue with cakephp, bascially i have a Has And Belongs To Many (HABTM) model relationship.
My models are Categroy and Project
bring all project data is fine, it comes out as [0]['Project']
, [1]['Project']
...etc
but when i use the relationship and pull out projects with certain categories in the categories controller i get these tpye of results [0] (all project data in [0] instead of [0]['Project']), [1] (project data and related model info) this is really messing my code up as i use one element view file to render my projects is there any way to return [0]['Project'] for both project controller and categories controller? thanks Chris
Hi sorry if my example isnt clear
i have projects and categories
when i pull a list of projects from the projects controller from my project model the results i get are in this format
[0]['Project'] = array(data...);
[1]['Project'] = array(data...);
[2]['Project'] = array(data...);
this is how the data is pulled and thats fine for me but when i pull projects per cetegory page using the HABTM relationship in the categories controller from the category model this is how my data is returned
['Project'][0] = array(data...);
['Project'][1] = array(data...);
['Project'][2] = array(data...);
which as you can see is a bit of a strain as i want to keep 1 element view file to display my projects, so far my view file prints data like so
<?php print $project['Project']['title']; ?> //data is returned [x]['Project']
<?php print $project['Feature']['title']; ?>
with the way the HABTM relationship is returning data i would need to do this
<?php print $project['title']; ?> //because data is returned ['Project'][x]
<?php print $project['Feature']['title']; ?>
can anyone help with this? thanks
This has frustrated me too. I like to have one set of elements that can be used for rendering both "primary" find results as well as related find results.
This is the way I currently deal with the differences in formats of results.
When calling find on, say, a "Project" model and wanting to render the related "Task" list, I run the "Task" key of the results through a function on its way into the element like so:
echo $this->element('tasks/index',array(
'data'=>make_primary('Task',$data['Task'])
));
My 'make_primary' function is like so:
function make_primary($alias,$data) {
$d = array();
foreach($data as $item) {
$related = array();
foreach($item as $key => $val) {
if(!is_numeric($key) && is_array($val)) {
$related[$key] = $val;
unset($item[$key]);
}
}
$d[] = array_merge(array($alias=>$item), $related);
}
return $d;
}
This returns a new array as though it was the result of a "primary" find query.
这篇关于Cakephp 1.3 HABTM问题! :D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!