0

我有一个使用 Codeigniter/Datamapper 的应用程序。有一个医生表,一个专业表,模型设置为多对多关系。

我注意到尝试按专业查询医生导致它只查询所有医生。以前有人遇到过这个问题吗?这是我正在使用的代码:

$s = new Specialty();
$s->where('id',$id)->get(); //thought maybe get_by_id($id) was causing the issue, it wasnt...
$this->out['query'] = $s->doctor->order_by('last_name','asc')->get_sql();
$docs = $s->doctor->order_by('id')->get_iterated();

$this->out['query'] 响应以下 sql 查询:

"SELECT * FROM (`doctors`) ORDER BY `doctors`.`last_name` asc"

另一个奇怪的事情是结果不是按姓氏排序的,但我假设它是如何将数组传递给 json_encode 函数的。

4

1 回答 1

0

您需要向该get_sql方法添加一些参数,因为您在相关对象上使用它。

文档

$object->get_sql()

This method returns the SQL for the currently built query, as if get() was called. It clears the current query immediately.

Arguments

$limit: (Optional) Sets the limit on the query.
$offset: (Optional) Sets the offset on the query.
$handle_related: (Optional) If TRUE, and this object is referenced from a parent object, the parent object will automatically to the where statement.


$u = new User();
$u->where('name', 'Bob');
echo $u->get_sql();
// outputs the raw SQL
Example with relationship
$group = new Group(1); // load Group #1

echo $group->user->get_sql();
// SELECT `users`.*
// FROM `users`

echo $group->user->get_sql(NULL, NULL, TRUE);
// SELECT `users`.*
// FROM `users`
// LEFT OUTER JOIN `groups` groups ON `users`.`group_id` = `groups.id`
// WHERE `groups`.`id` = 1

所以你需要这样做才能输出正确的查询:

$this->out['query'] = $s->doctor->order_by('last_name','asc')->get_sql(NULL, NULL, TRUE);
于 2013-01-04T22:12:27.707 回答