duanfei1987 2010-11-13 23:15
浏览 38
已采纳

Cakephp 1.3 HABTM结果

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

  • 写回答

1条回答 默认 最新

  • doumu8217 2010-11-14 11:30
    关注

    Your example is a bit hard to understand, but in general it's correct that records of related "-Many" models do not contain the model name in the array. I wouldn't recommend you to attempt to change that, just get used to how Cake fetches results and keep it consistent throughout the app. It's easy enough to make a view element or helper work with either format. Something along the lines of this:

    $projects = $category['Project'];
    if (isset($projects['Project'])) {
        $projects = $projects['Project'];
    }
    
    foreach ($projects as $project) {
        // treat $project as if it's always a flat array
    }
    

    Since I don't really understand your example, you'll have to adapt this a bit, but you get the idea...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗