我有一张这样的桌子.
[id] [name] [age]
1 foo 20
2 bar NULL
Run Code Online (Sandbox Code Playgroud)
如果我需要在age的字段为NULL时返回所有行,我会执行以下操作并且工作正常.
$this->db->from('person')->where('age', null);
Run Code Online (Sandbox Code Playgroud)
问题是我需要做相反的事情,我的意思是,当年龄字段不是空值时返回所有行.
在SQL中我做这样的事情
SELECT * FROM person WHERE age is not null
Run Code Online (Sandbox Code Playgroud)
我如何使用CodeIgniter ActiveRecord?
任何帮助赞赏.
alp*_*uri 11
你可以使用:
$this->db->from('person')->where('age is not null');
Run Code Online (Sandbox Code Playgroud)
如果你好奇这是怎么发生的 system/database/DB_active_rec.php
protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
{
...
if (is_null($v) && ! $this->_has_operator($k))
{
// value appears not to have been set, assign the test to IS NULL
$k .= ' IS NULL';
}
...
}
Run Code Online (Sandbox Code Playgroud)
和 system/database/DB_driver.php
/**
* Tests whether the string has an SQL operator
*
* @access private
* @param string
* @return bool
*/
function _has_operator($str)
{
$str = trim($str);
if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
{
return FALSE;
}
return TRUE;
}
Run Code Online (Sandbox Code Playgroud)
因此,您可以将第一个参数传递给where()方法,如果此方法的第一个运算符具有子字符串,is not null则它将保持不变.
阅读更多 - CodeIgniter论坛
| 归档时间: |
|
| 查看次数: |
18749 次 |
| 最近记录: |