Skip to content

Commit

Permalink
Related properties model lazy load
Browse files Browse the repository at this point in the history
  • Loading branch information
skeeks-semenov committed Jul 3, 2018
1 parent b51063a commit b518596
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 44 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
==============


5.1.3
----------------
* Related properties model lazy load

5.1.2
----------------
* Grid CSV export
Expand Down
186 changes: 143 additions & 43 deletions src/relatedProperties/models/RelatedPropertiesModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,59 +46,118 @@ class RelatedPropertiesModel extends DynamicModel
*/
private $_propertyValues = [];

public function init()


public function setAttributes($values, $safeOnly = true)
{
parent::init();
foreach ($values as $code => $value)
{
$this->hasAttribute($code);
}

if ($this->relatedElementModel->relatedProperties) {
foreach ($this->relatedElementModel->relatedProperties as $property) {
$this->defineAttribute($property->code, $property->handler->isMultiple ? [] : null);
parent::setAttributes($values, $safeOnly);
}

$property->relatedPropertiesModel = $this;
$property->addRules();
protected function _defineByProperty($property) {

$this->_properties[$property->code] = $property;
}
}
//if ($property = $this->relatedElementModel->getRelatedProperties()->andWhere(['code' => $code])->one()) {

if ($relatedElementProperties = $this->relatedElementModel->relatedElementProperties) {
foreach ($this->_properties as $code => $property) {
if ($property->handler->isMultiple) {
$values = [];
$valuesModels = [];

foreach ($relatedElementProperties as $propertyElementVal) {
if ($propertyElementVal->property_id == $property->id) {
$values[$propertyElementVal->id] = $propertyElementVal->value;
$valuesModels[$propertyElementVal->id] = $propertyElementVal;
}
}
/**
* @var $property RelatedPropertyModel
*/
$this->defineAttribute($property->code, $property->handler->isMultiple ? [] : null);

$values = $property->handler->initValue($values);
$property->relatedPropertiesModel = $this;
$property->addRules();

$this->setAttribute($code, $values);
$this->_propertyValues[$code] = $valuesModels;
} else {
$value = null;
$valueModel = null;
$this->{$property->code} = $property->defaultValue;

$this->_properties[$property->code] = $property;

$this->_attributeLabels[$property->code] = $property->name;
$this->_attributeHints[$property->code] = $property->hint;

foreach ($relatedElementProperties as $propertyElementVal) {
if ($propertyElementVal->property_id == $property->id) {
$value = $propertyElementVal->value;
$valueModel = $propertyElementVal;
break;
}
}

$value = $property->handler->initValue($value);
//$propertyValues = $this->relatedElementModel->getRelatedElementProperties()->where(['property_id' => $property->id])->all();

//}
}

$this->setAttribute($code, $value);
$this->_propertyValues[$code] = $valueModel;
/*public function toArray(array $fields = [], array $expand = [], $recursive = true)
{
$result = parent::toArray($fields, $expand, $recursive);
print_r($result);
die;
return $result;
}*/

/**
* @param RelatedPropertyModel $property
* @param array $relatedElementProperties
*/
protected function _initPropertyValue($property, $relatedElementProperties = []) {
$code = $property->code;
if ($property->handler->isMultiple) {
$values = [];
$valuesModels = [];

foreach ($relatedElementProperties as $propertyElementVal) {
if ($propertyElementVal->property_id == $property->id) {
$values[$propertyElementVal->id] = $propertyElementVal->value;
$valuesModels[$propertyElementVal->id] = $propertyElementVal;
}
}

$values = $property->handler->initValue($values);

$this->setAttribute($code, $values);
$this->_propertyValues[$code] = $valuesModels;
} else {
$value = null;
$valueModel = null;

foreach ($relatedElementProperties as $propertyElementVal) {
if ($propertyElementVal->property_id == $property->id) {
$value = $propertyElementVal->value;
$valueModel = $propertyElementVal;
break;
}
}

$value = $property->handler->initValue($value);

$this->setAttribute($code, $value);
$this->_propertyValues[$code] = $valueModel;
}
}

public function initAllProperties()
{
if ($this->relatedElementModel->relatedProperties) {
foreach ($this->relatedElementModel->relatedProperties as $property) {
$this->_defineByProperty($property);
}
}

if ($relatedElementProperties = $this->relatedElementModel->relatedElementProperties) {
foreach ($this->_properties as $code => $property) {
$this->_initPropertyValue($property, $relatedElementProperties);
}
}
}

public function init()
{
\Yii::beginProfile('init RP');

parent::init();
//$this->_initAllProperties();

\Yii::endProfile('init RP');
}


/**
* Saves the current record.
Expand Down Expand Up @@ -297,32 +356,39 @@ protected function _deleteRelatedPropertyValue($property)
return $this;
}

protected $_attributeLabels = [];
/**
* @inheritdoc
*/
public function attributeLabels()
{
$result = [];
return $this->_attributeLabels;

/*$result = [];
foreach ($this->relatedElementModel->relatedProperties as $property) {
$result[$property->code] = $property->name;
}
return $result;
return $result;*/
}

protected $_attributeHints = [];

/**
* @return array
*/
public function attributeHints()
{
$result = [];
return $this->_attributeHints;

/*$result = [];
foreach ($this->relatedElementModel->relatedProperties as $property) {
$result[$property->code] = $property->hint;
}
return $result;
return $result;*/
}

/**
Expand Down Expand Up @@ -364,6 +430,7 @@ public function loadDefaultValues($skipIfSet = true)
*/
public function getRelatedProperty($name)
{
$this->hasAttribute($name);
return ArrayHelper::getValue($this->_properties, $name);
}

Expand All @@ -373,18 +440,51 @@ public function getRelatedProperty($name)
*/
public function getRelatedElementProperties($name)
{
$this->hasAttribute($name);
return ArrayHelper::getValue($this->_propertyValues, $name);
}


/**
* {@inheritdoc}
*/
public function __get($name)
{
$this->hasAttribute($name);
return parent::__get($name);
}

/**
* {@inheritdoc}
*/
public function __set($name, $value)
{
$this->hasAttribute($name);
return parent::__set($name, $value);
}

/**
* Returns a value indicating whether the model has an attribute with the specified name.
* @param string $name the name of the attribute
* @return boolean whether the model has an attribute with the specified name.
*/
public function hasAttribute($name)
{
return in_array($name, $this->attributes());
if (in_array($name, $this->attributes())) {
return true;
}

if ($property = $this->relatedElementModel->getRelatedProperties()->andWhere(['code' => $name])->one()) {
$this->_defineByProperty($property);
$pv = $this->relatedElementModel->getRelatedElementProperties()->where(['property_id' => $property->id])->all();
$this->_initPropertyValue($property, (array) $pv);
}

if (in_array($name, $this->attributes())) {
return true;
}

return false;
}

/**
Expand Down

0 comments on commit b518596

Please sign in to comment.