Skip to content

Commit

Permalink
Create a non saved instance with 'instance' method
Browse files Browse the repository at this point in the history
  • Loading branch information
Zizaco committed Feb 17, 2013
1 parent 5ea52a1 commit 7c332cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
31 changes: 24 additions & 7 deletions src/Zizaco/FactoryMuff/Facade/FactoryMuff.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class FactoryMuff
* @access private
* @static
*/
private static $singletown;
private static $fmInstance;


/**
Expand All @@ -27,14 +27,14 @@ class FactoryMuff
*
* @return Zizaco\FactoryMuff\FactoryMuff
*/
private static function instance()
private static function fmInstance()
{
if (! static::$singletown)
if (! static::$fmInstance)
{
static::$singletown = new \Zizaco\FactoryMuff\FactoryMuff;
static::$fmInstance = new \Zizaco\FactoryMuff\FactoryMuff;
}

return static::$singletown;
return static::$fmInstance;
}

/**
Expand All @@ -51,7 +51,24 @@ private static function instance()
*/
public static function create( $model, $attr = array() )
{
return static::instance()->create( $model, $attr );
return static::fmInstance()->create( $model, $attr );
}

/**
* Creates and saves in db an instance
* of Model with mock attributes
*
* @param string $model Model class name.
* @param array $attr Model attributes.
*
* @access public
* @static
*
* @return mixed Returns the model instance.
*/
public static function instance( $model, $attr = array() )
{
return static::fmInstance()->instance( $model, $attr );
}

/**
Expand All @@ -68,6 +85,6 @@ public static function create( $model, $attr = array() )
*/
public static function attributesFor( $model, $attr = array() )
{
return static::instance()->attributesFor( $model, $attr );
return static::fmInstance()->attributesFor( $model, $attr );
}
}
11 changes: 9 additions & 2 deletions src/Zizaco/FactoryMuff/FactoryMuff.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ class FactoryMuff
* @return mixed Returns the model instance.
*/
public function create( $model, $attr = array() )
{
$obj = $this->instance( $model, $attr );

$obj->save();

return $obj;
}

public function instance( $model, $attr = array() )
{
// Get the factory attributes for that model
$attr_array = $this->attributesFor( $model, $attr );
Expand All @@ -56,8 +65,6 @@ public function create( $model, $attr = array() )
$obj->$attr = $value;
}

$obj->save();

return $obj;
}

Expand Down

0 comments on commit 7c332cd

Please sign in to comment.