Kohana 3.1 ORM:如何制作"where ... in"子句 [英] Kohana 3.1 ORM: How to make 'where ... in' clause

查看:100
本文介绍了Kohana 3.1 ORM:如何制作"where ... in"子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Kohana的出色文档,我不得不求助于SO. ;)

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

希望这真的很简单:我正在尝试收集属于某个ID组的所有故事.我的代码如下:

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();

但是,这显然是行不通的.我收到一个MySQL错误,因为在查询中的$story_ids字符串两边加上了单引号.

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.

编辑:我也尝试过将$story_ids作为数组传递,但随后我收到"500 Internal Server Error"(内部服务器错误)

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?

谢谢.

推荐答案

您可能忘记了-> select()吗?

此外,这里列出了两种方式这里使用"IN"关键字:

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();

我通常在执行操作时使用DB :: Expr方法.

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

这篇关于Kohana 3.1 ORM:如何制作"where ... in"子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆