0

Codeigniter/Datamapper を使用するアプリケーションがあります。Doctors 用のテーブルと Specialties 用のテーブルがあり、モデルは多対多の関係を持つように設定されています。

専門分野別に医師を照会しようとすると、すべての医師を照会するだけになることに気付きました。誰かが以前にこの問題を抱えていましたか? 私が使用しているコードは次のとおりです。

$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"

もう 1 つの奇妙な点は、結果が last_name の順序で返されないことですが、配列が 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 に答える