A small enhancement:
The where() method already provides a lot of conditions. However, I just came across the need to check in a query if a field is null or an empty string. Like empty() in PHP. Could this be done by Yii?
AFAIK one needs to create two annoying conditions to check for this. It would be nice if Yii could do this.
As an option you could also consider to trim the value first if this is possible.
Small MySQL example: http://stackoverflow.com/a/14575847/57091
NO
Check out Query::filterWhere().
I would create an IS_EMPTY function in MySQL.
->where('IS_EMPTY(col)')
@Alex-Code ISEMPTY() is only for geometry data, as far as I can tell.
@nineinchnick This is the other way around. But I'd like to check for the data in the table that might be NULL. Not the variable that I pass to filterWhere() is NULL.
@mdmunir NO?
Like empty() in PHP. Could this be done by Yii?
I said "NO". Mean, do it by your self.
@mdmunir Okay. I meant: Could this be done by Yii in a future version? This issue is an enhancement request.
->where("col1 IS NOT NULL AND TRIM(col1) <> ''") should work just fine.
Of course. I just thought it would be conveniont.
use LENGTH :
SELECT col1
FROM table
WHERE LENGTH(col1) > 0
Yii2 :
->where(['>', 'LENGTH(col1)', 0])
if someone need to get records from db with some empty field you can use:
/* @var $query ActiveQuery */
$query->andWhere([
'or',
['is', 'col_name', null],
['=', 'col_name', '']
]);
Most helpful comment
use LENGTH :
Yii2 :
->where(['>', 'LENGTH(col1)', 0])https://stackoverflow.com/a/48841766/2042775