Kohana 3.1 ORM: How to make 'where … in' clause

非 Y 不嫁゛ 提交于 2020-02-14 01:13:43

问题


Thanks to Kohana's excellent documentation, I'm having to resort to prostrate myself on SO. ;)

Hopefully this is really simple: I'm trying to gather all stories which belong to a certain group of IDs. My code is as follows:

$story_ids = '(12,56,99,213,319)';
$stories = ORM::factory('story')->where('id', 'IN', $story_ids)->find_all();

However, this is obviously not working. I'm getting a MySQL error because single-quotes are being put around the $story_ids string in the query.

EDIT: I've also tried passing $story_ids as an array, but then I just get a "500 Internal Server Error"

Is it possible to do what I'm asking?

Thanks in advance.


回答1:


Did you perhaps forget the ->select() ?

Also, here are two ways outlined here to use the "IN" keyword:

ORM::factory('table1')->select('mls_id')->where('mls_id', 'NOT IN', DB::Select('mls_id')->from('table2'))->find_all();
ORM::factory('table1')->select('mls_id')->where('mls_id', 'NOT IN', DB::Expr('(SELECT mls_id FROM table2)'))->find_all();

I typically use the DB::Expr method with what you're doing.




回答2:


Passing $story_ids as an array should work.

$story_ids = array(12,56,99,213,319);
$stories = ORM::factory('story')->where('id', 'IN', $story_ids)->find_all();

What Kohana version do you use?



来源:https://stackoverflow.com/questions/5643013/kohana-3-1-orm-how-to-make-where-in-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!