Skip to content
This repository has been archived by the owner on Sep 14, 2020. It is now read-only.

Commit

Permalink
Make it possible to set related models, not just plain values.
Browse files Browse the repository at this point in the history
  • Loading branch information
ddinchev committed Feb 19, 2015
1 parent ad502da commit aa5638a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/YiiFactoryGirl/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,20 @@ public function build($class, array $args = array(), $alias = null)
$factory = $this->getFactoryData($class);
$attributes = $factory->getAttributes($args, $alias);
foreach ($attributes as $key => $value) {
if ($obj->hasAttribute($key)) {
$obj->setAttribute($key, $value);
} else if ($reflection->hasProperty($key)) {
if ($reflection->hasProperty($key)) {
$property = $reflection->getProperty($key);
$property->setAccessible(true);
$property->setValue($value);
} else {
throw new FactoryException(\Yii::t(self::LOG_CATEGORY, 'Unknown attribute "{attr} for class {class}.', array(
'{attr}' => $key,
'{class}' => $class
)));
try {
$obj->__set($key, $value);
} catch(\CException $e) {
\Yii::log($e->getMessage(), \CLogger::LEVEL_ERROR);
throw new FactoryException(\Yii::t(self::LOG_CATEGORY, 'Unknown attribute "{attr} for class {class}.', array(
'{attr}' => $key,
'{class}' => $class
)));
}
}
}
return $obj;
Expand Down
2 changes: 1 addition & 1 deletion src/YiiFactoryGirl/FactoryData.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getAttributes($args = array(), $alias = null)
}
$attributes = array_merge($attributes, $args);
foreach ($attributes as $key => $value) {
$attributes[$key] = Sequence::get($value);
$attributes[$key] = is_object($value) ? $value : Sequence::get($value);
}
return $attributes;
}
Expand Down

0 comments on commit aa5638a

Please sign in to comment.