Created
May 13, 2014 06:28
-
-
Save cyhalothrin/743f76cb72df416b7b09 to your computer and use it in GitHub Desktop.
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
<?php | |
namespace app\models; | |
use Yii; | |
use app\models\ObjectPoperty; | |
/** | |
* This is the model class for table "objects". | |
* | |
* @property integer $id | |
* @property string $name | |
* @property string $type | |
* | |
* @property ObjectProperties[] $objectProperties | |
* @property ObjectRealtions[] $objectRealtions | |
*/ | |
class Object extends \yii\db\ActiveRecord | |
{ | |
/** | |
* | |
* @var string | |
* @eav | |
* @reference | |
*/ | |
public $eavProperty1; | |
/** | |
* | |
* @var type | |
* @eav | |
*/ | |
public $eavProperty2; | |
/** | |
* @inheritdoc | |
*/ | |
public static function tableName() | |
{ | |
return 'objects'; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function rules() | |
{ | |
return [ | |
]; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function attributeLabels() | |
{ | |
return [ | |
'id' => 'ID', | |
'name' => 'Name', | |
'type' => 'Type', | |
]; | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getObjectProperties() | |
{ | |
return $this->hasMany(ObjectPoperty::className(), ['object_id' => 'id']); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
// public function getObjectRealtions() | |
// { | |
// return $this->hasMany(ObjectRealtions::className(), ['right' => 'id']); | |
// } | |
public static function getEavProperties() | |
{ | |
/* @var $property \ReflectionProperty */ | |
$class = new \ReflectionClass(self::className()); | |
$names = []; | |
foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { | |
if (!$property->isStatic() && strpos($property->getDocComment(), '@eav') !== false) { | |
$names[] = $property->getName(); | |
} | |
} | |
return $names; | |
} | |
protected function getType() | |
{ | |
return 'object'; | |
} | |
public static function populateRecord($record, $row) | |
{ | |
parent::populateRecord($record, $row); | |
$eav = self::getEavProperties(); | |
foreach ($record->objectProperties as $property) { | |
if (in_array($property->name, $eav)) { | |
$record->{$property->name} = $property->getValue(); | |
} | |
} | |
} | |
public function beforeSave($insert) | |
{ | |
$this->type = $this->getType(); | |
return parent::beforeSave($insert); | |
} | |
public function save($runValidation = true, $attributeNames = NULL) | |
{ | |
$transaction = $this->getDb()->beginTransaction(); | |
try { | |
if (parent::save()) { | |
$transaction->commit(); | |
} | |
} catch (Exception $ex) { | |
$transaction->rollBack(); | |
throw $ex; | |
} | |
} | |
public function afterSave($insert) | |
{ | |
$this->saveProperties($insert); | |
parent::afterSave($insert); | |
} | |
protected function saveProperties($insert) | |
{ | |
if (!$insert) { | |
ObjectPoperty::deleteAll('object_id = :id', ['id' => $this->id]); | |
} | |
foreach (self::getEavProperties() as $key) { | |
$property = new ObjectPoperty(); | |
$property->object_id = $this->id; | |
$property->name = $key; | |
$comment = (new \ReflectionProperty($this, $key))->getDocComment(); | |
if (strpos($comment, '@reference') === false) { | |
$property->value = $this->$key; | |
} else { | |
$property->value_id = $this->$key; | |
} | |
if (!$property->save()) { | |
throw new \yii\base\Exception('Ошибка сохраения атрибута ' . $key); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment