-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- renamed methods: loadDynamicFields, saveDynamicFields, deleteDynamicFields. added BaseDynamicFieldsBehavior. added JsonDynamicFieldsBehavior.
- Loading branch information
Showing
3 changed files
with
144 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace chemezov\yii2_dynamic_fields; | ||
|
||
use yii\base\Behavior; | ||
|
||
abstract class BaseDynamicFieldsBehavior extends Behavior | ||
{ | ||
/** | ||
* Fields to store and load with your model. Example: ['address', 'is_client']. | ||
* | ||
* @var string[] | ||
*/ | ||
public $fields = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $_values = []; | ||
|
||
public function canGetProperty($name, $checkVars = true) | ||
{ | ||
return in_array($name, $this->fields); | ||
} | ||
|
||
public function canSetProperty($name, $checkVars = true) | ||
{ | ||
return in_array($name, $this->fields); | ||
} | ||
|
||
public function __get($name) | ||
{ | ||
if (array_key_exists($name, $this->_values)) { | ||
return $this->_values[$name]; | ||
} | ||
} | ||
|
||
public function __set($name, $value) | ||
{ | ||
if ($this->canSetProperty($name)) { | ||
$this->_values[$name] = $value; | ||
} | ||
} | ||
|
||
/** | ||
* Save dynamic fields from storage. | ||
*/ | ||
abstract public function saveDynamicFields(): void; | ||
|
||
/** | ||
* Load dynamic fields from storage. | ||
*/ | ||
abstract public function loadDynamicFields(): void; | ||
|
||
/** | ||
* Delete dynamic fields from storage. | ||
*/ | ||
abstract public function deleteDynamicFields(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace chemezov\yii2_dynamic_fields; | ||
|
||
use yii\db\ActiveRecord; | ||
use yii\helpers\Json; | ||
|
||
/** | ||
* Behavior to store dynamic fields in separate attribute of model. | ||
* | ||
* Class JsonDynamicFieldsBehavior | ||
* @package chemezov\yii2_dynamic_fields | ||
*/ | ||
class JsonDynamicFieldsBehavior extends BaseDynamicFieldsBehavior | ||
{ | ||
/** | ||
* Attribute name with json data. | ||
* | ||
* @var string | ||
*/ | ||
public $attributeName = 'additional_data'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function events() | ||
{ | ||
return [ | ||
ActiveRecord::EVENT_AFTER_FIND => 'loadDynamicFields', | ||
ActiveRecord::EVENT_BEFORE_INSERT => 'saveDynamicFields', | ||
ActiveRecord::EVENT_BEFORE_UPDATE => 'saveDynamicFields', | ||
ActiveRecord::EVENT_BEFORE_DELETE => 'deleteDynamicFields', | ||
]; | ||
} | ||
|
||
public function loadDynamicFields(): void | ||
{ | ||
$data = $this->owner->{$this->attributeName} ?: []; | ||
|
||
if (is_string($data)) { | ||
$data = Json::decode($data); | ||
} | ||
|
||
foreach ($data as $field => $value) { | ||
if (in_array($field, $this->fields)) { | ||
$this->owner->$field = $value; | ||
} | ||
} | ||
} | ||
|
||
public function saveDynamicFields(): void | ||
{ | ||
$this->owner->{$this->attributeName} = Json::encode($this->_values); | ||
} | ||
|
||
public function deleteDynamicFields(): void | ||
{ | ||
$this->_values = []; | ||
$this->owner->{$this->attributeName} = null; | ||
} | ||
} |