Skip to content

Commit

Permalink
Added 'insert' method to Mongolid\Model
Browse files Browse the repository at this point in the history
  • Loading branch information
Zizaco committed Jul 23, 2014
1 parent 17501c9 commit 22a942f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/Zizaco/Mongolid/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,55 @@ class Model
*/
public $guarded = array();

/**
* Insert the model to the database if the _id of it has not been used before
*
* @return bool
*/
public function insert()
{
// If the model has no collection. Aka: embeded model
if (! $this->collection) return false;

// If the "saving" event returns false we'll bail out of the save and return
// false, indicating that the save failed. This gives an opportunities to
// listeners to cancel save operations if validations fail or whatever.
if ($this->fireModelEvent('saving') === false) {
return false;
}

// If the model has not an _id then fire the creating event
if ($this->fireModelEvent('creating') === false) {
return false;
}

// Prepare the created_at and updated_at attributes for the given model
$this->prepareTimestamps();

// Prepare the attributes of the model
$preparedAttr = $this->prepareMongoAttributes( $this->attributes );

// Saves the model using the MongoClient
$result = $this->collection()
->insert( $preparedAttr, array("w" => $this->writeConcern) );

if (isset($result['ok']) && $result['ok'] ) {

// the created event is fired, just in case the developer tries to update it
// during the event. This will allow them to do so and run an update here.
$this->parseDocument($this->attributes);
$this->fireModelEvent('created', false);

// The "saved" event will always be fired when inserting or updating an model
// instance.
$this->fireModelEvent('saved', false);

return true;
} else {
return false;
}
}

/**
* Save the model to the database.
*
Expand Down Expand Up @@ -123,6 +172,8 @@ public function save()
// Prepare the attributes of the model
$preparedAttr = $this->prepareMongoAttributes( $this->attributes );

//var_dump($preparedAttr);

// Saves the model using the MongoClient
$result = $this->collection()
->save( $preparedAttr, array("w" => $this->writeConcern) );
Expand Down
19 changes: 18 additions & 1 deletion tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ public function tearDown()
_stubCategory::$connection = null;
}

public function testShouldInsert()
{
$prod = new _stubProduct;
$prod->name = 'Something';

$this->productsCollection
->shouldReceive('insert')
->with(
m::any(),
['w'=>1]
)
->once()
->andReturn(['ok'=>1]);

$this->assertTrue($prod->insert());
}

public function testShouldSave()
{
$prod = new _stubProduct;
Expand All @@ -43,7 +60,7 @@ public function testShouldSave()
$this->productsCollection
->shouldReceive('save')
->with(
$this->prepareMongoAttributes( $prod->attributes ),
m::any(),
['w'=>1]
)
->once()
Expand Down

0 comments on commit 22a942f

Please sign in to comment.