notorm icon indicating copy to clipboard operation
notorm copied to clipboard

query for columns that are not null

Open brachetti opened this issue 9 years ago • 8 comments

What would be a good way to query for a column that IS NOT NULL? From reading the code I can see no way to do that.

While waiting for an answer I will prepare a PR how I would do it.

Cheers! :-)

brachetti avatar Oct 20 '15 08:10 brachetti

Hi,

Did you tried $db->table()->where('column_name IS NOT NULL')?

vbarbarosh avatar Oct 20 '15 08:10 vbarbarosh

That actually seems to work, judging from the tests. I'm not entirely sure, why, though ...

Since the code I have to maintain works very extensively with arrays, I would like to explicitly add where("column", "is not null") ... what do you think?

brachetti avatar Oct 20 '15 08:10 brachetti

How then you distinct when column contains string "IS NOT NULL" and column literally IS NOT NULL?

vbarbarosh avatar Oct 20 '15 10:10 vbarbarosh

That's a good point you make there. Intuitively I would answer that with $table->where($col, "'IS NOT NULL'"); but it's not really ideal considering backwards compatibility ... do you have a suggestion using the array syntax?

brachetti avatar Oct 20 '15 13:10 brachetti

Just a quick thought: $db->table()->where($col, new IsNotNull()). Then at the place where is implemented check its second argument for instanceof IsNotNull.

vbarbarosh avatar Oct 20 '15 18:10 vbarbarosh

Hi,

This might work:

$db->table()->where("NOT $column", null);

On Tue, Oct 20, 2015 at 8:29 PM, Vladimir Barbarosh < [email protected]> wrote:

Just a quick thought: $db->table()->where($col, new IsNotNull()). Then at the place where is implemented check its second argument for instanceof IsNotNull.

— Reply to this email directly or view it on GitHub https://github.com/vrana/notorm/issues/109#issuecomment-149657366.

WooterTheTroubleshooter avatar Oct 20 '15 20:10 WooterTheTroubleshooter

Right from the docs. )

$table->where("NOT field", array("x", "y")) Translated to NOT field IN ('x', 'y') (with automatic escaping)

vbarbarosh avatar Oct 21 '15 04:10 vbarbarosh

So I guess it would be better to remove the new code in Result.php and just add more tests. I also think that the documentation could be enhanced in that regard. Is there a repository where I could do that, which I missed?

brachetti avatar Oct 21 '15 16:10 brachetti