douzhaobo6488 2013-06-26 17:42
浏览 20
已采纳

DataMapper通过join获得结果

I'm working on a demo app to evaluate CodeIgniter for a new project. We are currently reviewing the DataMapper ORM Library - http://datamapper.wanwizard.eu

I've hit a problem that seems odd: I cannot get values from 2 joined tables in one return. I have created models for both tables and verified that I have the $has_many/$has_one set properly. In my controller this code produces an object with all the rows from the named table object:

$job = new Job();
$data['job'] = $job->get(); //produces obj with all rows from table 'jobs'
$acct = new Acct();
$data['acct'] = $acct->get(); //produces obj with all rows from table 'accts'

I have verified both of the above by looping over them with a FOREACH and viewing the data. But when I try to pull related info from both tables. I get nothing. I've tried everything I could find on GET(Advanced) docs. Specifically this is not working:

//NOTE: a job has one acct joined on field 'acct_id'
$job = new Job();
$data['job'] = $job->include_related('acct',array('name'))->get();

The result object ($data['job']) only contains the data from the 'jobs' table.

In my view I'm using this code in a view to see the data:

<table>
<tr><?php foreach ($job->fields as $col) { ?><th><?= $col ?></th><?php } ?></tr>
<?php foreach($job as $row) { ?>
<tr><?php foreach($job->fields as $col) { ?><td><?= $row->$col ?></td><?php } ?></tr>
<?php } ?>
</table>
  • 写回答

1条回答 默认 最新

  • dongyijing2353 2013-07-25 11:06
    关注

    Please try

    $job = new Job();

    $data['job'] = $job->include_related('acct',array('name'),TRUE,TRUE)->get();

    Detailed Description: http://datamapper.wanwizard.eu/pages/getadvanced.html#include_related

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

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题