dongmaxi6763 2011-01-22 04:14
浏览 41
已采纳

Cakephp:模型中的多个属性

In a model, is it possible to do multiple iterations of belongsTo?

Assuming 3 tables, alerts, schedules, tasks and the model is for alerts. I want to get at a field from tasks but I have to join through schedules.

 alerts.schedule_id -> schedules.tasks_id -> tasks.name

I tried this syntax:

var $belongsTo = array( 
         'Schedule' => array( 
             'className' => 'Schedule', 
             'foreignKey' => 'schedule_id' 
         ), 
         'Task' => array( 
             'className' => 'Task', 
             'foreignKey' => 'task_id' 
         ));

But that just joins both Schedules and Tasks directly to Alerts (here's the sql that was generated:

LEFT JOIN `schedules` AS `Schedule` ON (`Alert`.`schedule_id` = `Schedule`.`id`) LEFT JOIN `tasks` AS `Task` ON (`Alert`.`task_id` = `Task`.`id`)
)
  • 写回答

2条回答 默认 最新

  • doubi6303 2011-01-22 18:11
    关注

    I ended up setting this property:

    $this->Alert->recursive = 2;
    

    In the index() function of the alerts controller.

    Then I didn't have to do a separate find and was able to reference the name field in the Tasks table in index.ctp like this:

    <td><?php echo $alert['Schedule']['Task']['name']; ?>&nbsp;</td>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动