public function setAttributes($bunchOfAttributes) { parent::setAttributes($bunchOfAttributes); $relationClass = $this->info[WFormRelation::RELATION_CLASS]; $relationPk = $relationClass::model()->getMetaData()->tableSchema->primaryKey; $modelsDictionary = array(); foreach ($this->getRelatedModels() as $relationModel) { if ($relationModel->primaryKey) { $modelsDictionary[$relationModel->primaryKey] = $relationModel; } } $relationModels = array(); foreach ($bunchOfAttributes as $key => $attributes) { if (isset($attributes[$relationPk])) { if (isset($modelsDictionary[$attributes[$relationPk]])) { $relationModel = $modelsDictionary[$attributes[$relationPk]]; } else { $relationModel = $relationClass::model()->findByPk($attributes[$relationPk]) ?: new $relationClass(); } } else { $relationModel = new $relationClass(); } $relationModel->attributes = $attributes; $relationModels[$key] = $relationModel; } $this->model->{$this->name} = $relationModels; }
public function setAttributes($attributes) { parent::setAttributes($attributes); if (!is_null($attributes)) { $relationModel = $this->getRelatedModel(); $relationModel->attributes = $attributes; } else { $this->model->{$this->name} = null; } }
/** * @covers WFormRelation::getRelationInfo */ public function testGetRelationInfo() { $model = $this->getModelMock(); $this->assertNotNull(WFormRelation::getRelationInfo($model, 'hasOne')); $this->assertNotNull(WFormRelation::getRelationInfo($model, 'hasMany')); $this->assertNotNull(WFormRelation::getRelationInfo($model, 'manyMany')); $this->assertNotNull(WFormRelation::getRelationInfo($model, 'belongsTo')); $this->assertNotNull(WFormRelation::getRelationInfo($model, 'stat')); $this->assertNull(WFormRelation::getRelationInfo($model, 'misteriousRelation')); }
/** * @covers WFormRelationHasMany::getActualRelatedModels */ public function testGetActualRelatedModels() { $product = $this->_getProductWithRelation(1); $relation = WFormRelation::getInstance($product, 'images'); $this->assertCount(1, $relation->getRelatedModels()); $this->assertCount(1, $relation->getActualRelatedModels()); $product->attributes = array('name' => 'name', 'images' => array()); $this->assertCount(1, $relation->getActualRelatedModels()); $this->assertCount(0, $relation->getRelatedModels()); $product->attributes = array('name' => 'name', 'images' => array(array('file' => 'newfile1.txt'), array('file' => 'newfile2.txt'))); $this->assertCount(2, $relation->getRelatedModels()); $this->assertCount(1, $relation->getActualRelatedModels()); }
/** * @covers WFormRelationHasOne::getActualRelatedModel */ public function testGetActualRelatedModel() { $product = $this->_getProductWithRelation(1, array('required' => false)); $relation = WFormRelation::getInstance($product, 'description', array('required' => false)); $this->assertNotEmpty($relation->getRelatedModel(false)); $this->assertNotEmpty($relation->getActualRelatedModel()); $product->attributes = array('name' => 'name', 'description' => null); $this->assertNotEmpty($relation->getActualRelatedModel()); $this->assertEmpty($relation->getRelatedModel(false)); $product->attributes = array('name' => 'name', 'description' => array('size' => '10')); $this->assertNotEmpty($relation->getActualRelatedModel()); $this->assertNotEmpty($relation->getRelatedModel(false)); }
/** * Rebuild related models * * @param $parentModel * @return void */ protected function _buildRelatedModel($parentModel) { $this->relatedModels = array(); if (is_null($this->relations)) { $this->relations = array_keys($parentModel->relations()); } foreach ($this->relations as $index => $options) { $relation = $index; if (is_numeric($index)) { $relation = $options; $options = array(); } if (($relationModel = WFormRelation::getInstance($parentModel, $relation, $options)) !== null) { $this->relatedModels[$relation] = $relationModel; $this->relatedModels[$relation]->setPreloaded(in_array($relation, self::$preloadedRelations)); } else { unset($this->relations[$index]); } } }
/** * @covers WFormRelationHasMany::getRelatedModel */ public function testGetRelatedModel() { $product = $this->_getProductWithRelation(); $relation = WFormRelation::getInstance($product, 'category'); $this->assertEmpty($relation->getRelatedModel(false)); // $this->assertNotEmpty($relation->getRelatedModel(true)); $product->attributes = array('name' => 'product_name', 'category' => array('id' => 1, 'name' => '10')); $this->assertNotEmpty($relation->getRelatedModel()); $product = $this->_getProductWithRelation(1); $relation = WFormRelation::getInstance($product, 'category'); $this->assertNotEmpty($relation->getRelatedModel()); }