Key Value Storage

This function returns data stored in a table in key=>value

It is sometimes very useful to use a storage of data in a table key value .

For example, consider the table structure as follows :


CREATE TABLE IF NOT EXISTS `gen_object` ( 
`id_object` mediumint(8) NOT NULL AUTO_INCREMENT, 
`lat` float DEFAULT NULL, 
`lon` float DEFAULT NULL, 
`created_at` datetime NOT NULL, 
`updated_at` datetime NOT NULL, 
PRIMARY KEY (`id_object`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; 

-- -------------------------------------------------------- 

-- 
-- Structure de la table `gen_object_data` 
-- 

CREATE TABLE IF NOT EXISTS `gen_object_data` ( 
`id_object_data` mediumint(8) NOT NULL AUTO_INCREMENT, 
`id_object` mediumint(8) NOT NULL, 
`title` varchar(255) NOT NULL, 
`value` varchar(255) NOT NULL, 
PRIMARY KEY (`id_object_data`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; 

The goal here is to store a number of property N for each object. As the number of property is not fixed, we can not pass by a classic structure. Each object can have different properties. This is where the KeyValueStorage is very useful.

Each module extends from the Genclass can use this "engine" of storage in the following way ;


$KVS_Properties = array ( 
'primary_key' => 'id_object' 
,'parent_tablename' => 'gen_object' 
,'child_tablename' => 'gen_object_data' 
,'child_keyfield' => 'title' 
,'child_valuefield' => 'value' 
,'keys' => array( 
'departure' 
'cabinet' 
,'features' 
,'id' 
,'mark' 
,'model' 
,'permanent' 
,'power' 
,'sati' 
,'standard' 
,'type' 
,'pictogram' 
,'status' 
,'building' 
,'floor' 
,'lamp_power' 
,'lamp_type' 
,'voltage' 
) 
); 

print_r( $this->collector['object_data']->getKeyValueStore((int)$_GET['id_object'],$KVS_Properties) ); 

And here is the result for an object :

To retrieve these keys, you can just do a :


SELECT DISTINCT('title') FROM gen_object_data