Skip to content

Instantly share code, notes, and snippets.

@lukeholder
Created February 22, 2013 08:00
Show Gist options
  • Save lukeholder/5011641 to your computer and use it in GitHub Desktop.
Save lukeholder/5011641 to your computer and use it in GitHub Desktop.
working
<?php
namespace BlocksCommerce\Models;
use Blocks\AttributeType;
use Blocks\Blocks;
class PaymentMethodModel extends BaseModel
{
public function __toString()
{
return $this->name;
}
public function defineAttributes()
{
return array(
'id' => AttributeType::Number,
'name' => AttributeType::String,
'class' => AttributeType::String,
'enabled' => AttributeType::Bool,
'config' => array(AttributeType::String, 'column' => ColumnType::Text)
);
}
public function beforeSave($event)
{
$this->serialize('config');
parent::beforeSave();
}
public function afterSave($event)
{
$this->deserialize('config');
parent::afterSave();
}
public function afterFind($event)
{
$this->deserialize('config');
parent::afterFind();
}
private function deserialize($attribute)
{
$att = $this->getAttribute($attribute);
if(!empty($att) && is_scalar($att)) {
$a = @$this->jsonToArray($att);
if($a !== false) {
$this->setAttribute($attribute, $a);
} else {
$this->setAttribute($attribute, $this->arrayToJson(array()));
}
}
}
private function serialize($attribute)
{
$att = $this->getAttribute($attribute);
if(is_array($att)){
$this->setAttribute($attribute, $this->jsonToArray($att));
}else{
$this->setAttribute($attribute, $this->arrayToJson(array()));
}
}
private function jsonToArray($json){
return json_decode($json,true);
}
private function arrayToJson($array){
return json_encode($array);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment