CREATE TABLE `storage` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`key` varchar(255) DEFAULT NULL,
`value` text,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`,`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
public function __get($name)
{
try{
return parent::__get($name);
}
catch(Exception $e){
$sql = 'SELECT `s`.`value` FROM `storage` AS `s` WHERE `s`.`user_id` = :uid AND `key` = :key ';
return Yii::$app->db->createCommand($sql)->bindValues([
':uid' => $userId,
':key' => $name
])->queryScalar();
}
}
public function __set($name, $value)
{
try{
parent::__set($name, $value);
}
catch(Exception $e){
if($this->$name){
$sql = 'UPDATE `storage` AS `s` SET `s`.`value` = :value WHERE `s`.`key` = :key AND `s`.`user_id` = :uid ';
}
else{
$sql = 'INSERT INTO `storage` (`user_id`,`key`,`value`) VALUES (:uid,:key,:value)';
}
Yii::$app->db->createCommand($sql)->bindValues([
':value' => $value,
':key' => $name,
':uid' => $userId
])->execute();
}
}
$userId идентификатор нужного пользователя