CakePHP 3.x:查询以排除字段可以为NULL或为空('')的记录

Ash*_*ary 2 php sql cakephp sql-server-2008 cakephp-3.0

我正在使用Cakephp 3并尝试从一个字段为非空的表中获取一些行/记录.

例如:-

我有一个areas名为字段的表Area_block.我需要获取那些里面有东西的行的记录Area_block.简单来说,我不需要空Area_block行或NULL 行.

Area_block默认情况下不是NULL字段.所以它仍然是空的/空白.

我试过了

$conditions = ['Area_Block IS NOT NULL'];
$conditions = ['NOT' => array('Area_Block' => '')];
$conditions = ['Area_Block <>' => ''];
$conditions = ['Area_Block !=' => ''];
Run Code Online (Sandbox Code Playgroud)

但没有任何作用!!

我的环境包括Apache上的MSSQL server 2008和PHP 5.6.

请帮忙 !!

Ini*_*res 7

我知道你的表保存了Area_Block设置为NULL''(空字符串)的记录.

请尝试以下方法:

$conditions = [['Area_Block IS NOT' => null], ['Area_Block IS NOT' => '']];
Run Code Online (Sandbox Code Playgroud)

这应该也有效:

$conditions = ['Area_Block IS NOT' => [null,'']]; //haven't tested it
Run Code Online (Sandbox Code Playgroud)

但是,为了降低复杂性,您可能希望在数据库中将Area_Block字段设置为默认值NULL,或者默认设置为''阻止字段接受NULL值.

专门使用NOT NULL值可能更容易.如果您更改表格以反映这一点,您只需要检查''值:

$conditions = ['Area_Block IS NOT' => ''];
Run Code Online (Sandbox Code Playgroud)

请参阅3.x Cookbook中的自动IS NOT NULL创建.