I'd like to exclude results from a call to a Lithium model's find()
method. I need to do this for models with both MongoDB and MySQL data sources, but in SQL I mean something like WHERE myfield NOT IN (1,2,3)
.
I'd like to just be able to pass a not
clause in the conditions
array like below, but that doesn't appear to be possible.
Item::all(array('conditions' => array('not' => array('myfield' => array(1,2,3))));
So my question is, is this possible in Lithium in a way that I've overlooked? And if not, what would be the most Lithium-ish way to implement it for my models?
Merely filtering for MongoDB can easily be achieved like this:
If this is something you do a lot you could even create a custom finder for it :
And call it like this :
In the same manner you could create a custom finder for MySQL sources.
As you probably can see this creates some issues if you pass something like
array('fields'=>$array)
as it would overwrite the option. What happens is that::notin()
(finders in general) has a distinct behavior for the (array,null) signature. If that happens it thinks the first array is options and the finder took no arguments. Usingnotin($array,array())
breaks the previous finder because the first argument ends up in$params['notin']
when the real second argument (options) is passed.If you mix data sources on the fly here I would create a custom model that does not inherit \lithium\data\Model and have it delegate
to the different models and create the conditions based on the end models data source.
(Code might be slightly incorrect as its mostly taken from the top of my head)