簡體   English   中英

Codeigniter / Datamapper無法正確生成關系查詢

[英]Codeigniter/Datamapper not generating relationship query correctly

我有一個使用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"

另一個奇怪的是,結果並沒有按last_name的順序返回,但是我假設它是如何將數組傳遞給json_encode函數的。

您需要在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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM