Uniqueness in Fuel Models
As a part of broadening my horizons, I'm back to working with Fuel, a framework that has a familiar feel to some of the ones I've used in the past but has some extra a€ooomfa€¯ from new features and PHP 5.3-ness.
I was working in my models the other day and had a need for uniqueness - I wanted to be sure that the object I was pushing into the database didn't match any other one. I went through the docs and didn't see anything about it and a glance through the source didn't turn up anything either. So, as an alternative, I came up with a method I put in my base model:
class Model_Base extends Orm\Model { public function isUnique($modelObject) { $modelType = get_class($modelObject); $found = $modelType::find('all',array('where' = $modelObject-to_array())); return (count($found)0) ? false : true; } }The code above finds the class name (the model the object is made from) and tries to find anything with exactly the same properties. The a€oto_array()a€¯ method is something Fuel has to translate objects into handy arrays.


