$this->password = md5($password); } } # # Easy way - facade method (recommended) # $storage = M('Store')->storage(array('table' => 'user', 'props' => qw2('id>id_user nick>nick_name password'), 'keys' => qw2('id nick'), 'auto-id-prop' => 'id', 'class' => 'MyUserClass')); # # In-depth way - step-by-step # $mediator = new RM_Store_Object_Mediator(); $mediator->setMeta(new RM_Store_Object_Metadata(qw2('id>id_user nick>nick_name password'), qw2('id nick'))); # WARNING! Please note ALIAS using, NOT DB-fields! $mediator->setIdentityMap(new RM_Store_IdentityMap_Simple($mediator)); $mediator->setFactory(new RM_Store_Factory_Hash($mediator, 'MyUserClass')); $mediator->setStorage(new RM_Store_Object_Storage($mediator, array('table' => 'user', 'auto-id-prop' => 'id'))); $storage = $mediator->storage; # # Usage # # create new user $user1 = $storage->createObject(qw2('nick>vasya01 name>Vasya')); $user1->setPassword('qwerty'); # save user $user1->save(); # or $storage->saveObject($user); # quering users: # high-level (request) foreach ($storage->getObjectList()->sort('nick') as $user) { echo $user->name;
/** * Main method :) * Creates and initialize set of objects for proper storage functioning. Accepts hash of following parameters: * * Name Type Default Description * ---- ---- ------- ----------- * table string Table name * props hash<prop:db> Object properies and DB mappings. See RM_Store_Object_Metadata for details * keys list|hash 'id' Keys. See RM_Store_Object_Metadata for details. Sample: qw2('id name>parent_id+name') * auto-id-prop string NULL Name of field to fill with AUTO_INCREMENT value. If NULL - AUTO_INCREMENT is disabled * guid string NULL Name of GUID type to auto alloc ID. If NULL - GUID is disabled. * Also passed as "guidType" parameter to object constructor. * guid-prop string 'id' Name of GUID property. Used only if guid is not NULL * guid-type-prop string 'guid_type' Name of GUID-TYPE property. Used only if guid is not NULL * Passed as "guidTypeProp" parameter to object constructor. * class string RM_Store_Object Class which will be instantiated for every object in storage. * If class is RM_Store_Object and GUID is enabled - * RM_Store_Object_Guid is used * cache-prefix string '' Cache prefix for caching loadById() requests. * If empty(default), caching is disabled * validator RM_Validator_iValidator NULL Validator for every created object * factory RM_Store_iFactory|string NULL Factory for creating objects. Default is RM_Store_Factory_Hash. See it doc for details. * You can pass either object or class name. Class constructor must be compatible with * RM_Store_Factory_Hash constructor. * no-lazy-load bool FALSE Always fetch all object fields in queries for objects sets * ctorArgs hash NULL Extra arguments to pass to object constructor. * For every item, property with same name AND PREFIXED * WITH UNDERSCORE! (_prop) will created! * WARNING! Properties with such names must exist in class. * * All parameters without default values ARE REQUIRED! * * @param args hash<name:value> Configuration * @return RM_Store_Object_Storage */ public function storage($args) { $mediator = new RM_Store_Object_Mediator(); # Meta data $mediator->setMeta(new RM_Store_Object_Metadata($args['props'], lor(@$args['keys'], qw2('id')))); # IdentityMap $mediator->setIdentityMap($this->_noCache ? new RM_Store_IdentityMap_Fake() : new RM_Store_IdentityMap_Simple($mediator)); # Validator if (@$args['validator']) { $mediator->setValidator($args['validator']); } # Factory if (@$args['guid']) { $args['ctorArgs']['guidType'] = $args['guid']; $args['ctorArgs']['guidTypeProp'] = @$args['guid-type-prop']; if (@$args['class'] == '') { $args['class'] = 'RM_Store_Object_Guid'; } } if (!@$args['factory']) { $args['factory'] = 'RM_Store_Factory_Hash'; } if (!is_object($args['factory'])) { $args['factory'] = new $args['factory']($mediator, lor(@$args['class'], 'RM_Store_Object'), @$args['ctorArgs']); } if (@$args['guid']) { $args['factory'] = $this->factoryGuid($args['factory'], @$args['guid'], lor(@$args['guid-prop'], 'id')); } $mediator->setFactory($args['factory']); # Storage $mediator->setStorage(new RM_Store_Object_Storage($mediator, array('table' => $args['table'], 'table-history' => @$args['table-history'] ? $args['table-history'] : null, 'auto-id-prop' => @$args['auto-id-prop'], 'cache-prefix' => $this->_noIdCache ? '' : @$args['cache-prefix'], 'no-lazy-load' => lor(@$args['no-lazy-load'], $this->_noLazyLoad)))); return $mediator->storage; }