-
Notifications
You must be signed in to change notification settings - Fork 0
Object management
- Object Collection
- Main Object Collection
- Object serialization
- Object deserialization
- Object export
- Object import
- import via Object instance
- import via Model instance
An Object Collection permit to store objects and find them in constant time. Objects are grouped by model and by id. Obviously an object without id property cannot be stored in Object Collection.
Example of an Object Collection structure :
[
person : [
an_id_one : person_object_one,
an_id_two : person_object_two
],
town : [
an_id_one : town_object_one
],
house : [
an_id_one : house_object_one,
an_id_two : house_object_two
]
]
Example of usage :
$lPersonOne; // consider object already exists, has a model Person and has an id 'one'
$objectCollection = new OjectCollection();
$objectCollection->addObject($lPersonOne);
$objectCollection->hasObject('one', 'Person'); // return true
$object = $objectCollection->getObject('one', 'Person'); // return object previously added
$objectCollection->hasObject('two', 'Person'); // return false
$object = $objectCollection->getObject('two', 'Person'); // return null
$objectCollection->hasObject('one', 'House'); // return false
$object = $objectCollection->getObject('one', 'House'); // return nullMainObjectCollection is a singleton that store all deserialized (loaded) and imported objects. This singleton is usefull to know which object already exists and avoid redundants deserializations.
$object = MainObjectCollection::getInstance()->getObject('an_id', 'a_model_name');Whatever is your serialzation you just have to call one function save() to serialize your object. Obviously the model associated to your object must have a serialization.
$object->save();### Your table has an incremental id
Like other serializations call save() function but there are two cases to identify :
- if your Object doesn't have id set, an
INSERTwill be executed. ifINSERTis successfull, id will be set in your object. - if Object has id set, an
UPDATEwill be executed
### Your table doesn't have an incremental id
you must specify your operation (insert or update)
$object->save(SqlTable::INSERT);
$object->save(SqlTable::UPDATE);# Object deserialization
Whatever is your serialzation you just have to call one function loadObject() to serialize your object. Obviously the model associated to your object must have a serialization. When you load an object this object is stored in MainObjectCollection.
$object = $modelOne->loadObject('12');
// if your model has several id properties you must specify theme in a json encoded array
// order of values must be the same as order of id properties in manifest
$object = $modelTwo->loadObject('[1,1501774389]');Thanks to MainObjectCollection if an object has already been loaded it will not be loaded again (except if you force it).
// load object
$object = $modelOne->loadObject('12');
// return same instance object retrieve from MainObjectCollection
$object = $modelOne->loadObject('12');
// like previous instruction but force to call deserialization
// and merge current object instance with serialized object
$object = $modelOne->loadObject('12', true);# Object export You can export object to three formats :
- json (stdClass)
- XML (SimpleXMLElement)
- associative array which each value is a string (like format of an sql database SELECT request)
respectively call functions :
toObject()toXml()toSqlDataBase()
$person = new Object('person');
$person->setValue('id', 1);
$person->setValue('first_name', 'alexander');
print json_encode($person->toOject()); // output '{"id":1,"first_name":"alexander"}'# Object import You can import object via three formats :
- json (stdClass)
- XML (SimpleXMLElement)
- associative array which each value is a string (result of a sql database SELECT request)
respectively call functions :
fromObject()fromXml()fromSqlDataBase()
$person = new Object('person');
$person->setValue('id', 1);
$person->setValue('first_name', 'alexander');
// merge object with values to import
person->fromOject(json_decode('{"id":1,"first_name":"john","last_name":"doe"}'));
print json_encode($person->toOject()); // output '{"id":1,"first_name":"john","last_name":"doe"}'When you import an object via model this object is stored in MainObjectCollection.
$personModel = InstanceModel::getInstance()->getInstanceModel('person');
$person = $personModel->fromOject(json_decode('{"id":1,"first_name":"john","last_name":"doe"}'));when you import object via model you can configure your import type
// merge (type merge is used by default if you don't specify it)
// if an object with same model and same id is found in MainObjectCollection get this instance
// otherwise create a new one
// finally merge retrieved (or created) object with values to import
$person = $personModel->fromOject(json_decode('{"id":1,"first_name":"john","last_name":"doe"}'), Model::MERGE);
// overwrite
// if an object with same model and same id is found in MainObjectCollection get this instance and erase all values
// otherwise create a new one
// finally fill retrieved (or created) object with values to import
$person = $personModel->fromOject(json_decode('{"id":1,"first_name":"john","last_name":"doe"}'), Model::OVERWRITE);
// no merge
// create a new instance even if an instance exists in MainObjectCollection
// finally fill created object with values to import
$person = $personModel->fromOject(json_decode('{"id":1,"first_name":"john","last_name":"doe"}'), Model::NO_MERGE);