Skip to content

Commit

Permalink
Breaking Change: stop automatically handling string representations o…
Browse files Browse the repository at this point in the history
…f MongoIds
  • Loading branch information
julien-c committed Oct 16, 2014
1 parent 014a983 commit 42510c7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Book extends MongovelModel
```php
public function show($id)
{
$book = Book::findOne($id);
$book = Book::findOne(new MongoId($id));

// Here, you can access the book's attributes like in Eloquent:
// $book->title, $book->reviews, etc.
Expand All @@ -35,7 +35,7 @@ public function show($id)
}
```

Mongovel detects that `$id` is the string representation for a MongoId, and returns an object that will be automatically serialized and sent as JSON by Laravel.
Mongovel detects that `$id` is a MongoId, and returns an object that will be automatically serialized and sent as JSON by Laravel.

**`POST books`**:

Expand All @@ -58,7 +58,7 @@ public function reviewStore($id)
$review = Input::all();

// You can leverage the full power of Mongo query operators:
Book::update($id,
Book::update(new MongoId($id),
array('$push' => array('reviews' => $reviews))
);

Expand All @@ -70,7 +70,7 @@ Deleting a book is as simple as:
```php
public function destroy($id)
{
Book::remove($id);
Book::remove(new MongoId($id));
}
```

Expand Down
9 changes: 3 additions & 6 deletions src/Mongovel/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,15 @@ public function jsonSerialize()
////////////////////////////////////////////////////////////////////

/**
* Magically handles MongoIds when passed as strings or objects
* Magically handles MongoIds when passed as objects
*
* @param string|array|MongoId $p An array of parameters or a MongoId (string/object)
* @param array|MongoId $p An array of parameters or a MongoId
*
* @return array
*/
protected static function handleParameters($p)
{
// Assume it's a MongoId
if (is_string($p) && strlen($p) === 24 && ctype_xdigit($p)) {
return array('_id' => new MongoId($p));
} elseif ($p instanceof MongoId) {
if ($p instanceof MongoId) {
return array('_id' => $p);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

class ModelTest extends MongovelTests
{
public function testCanFindOneByIdString()
public function testCanFindOneById()
{
self::$db->db()->books->insert(array(
'_id' => new MongoId('512ce86b98dee4a87a000000'),
'title' => "My life"
));

$book = Book::findOne('512ce86b98dee4a87a000000');
$book = Book::findOne(new MongoId('512ce86b98dee4a87a000000'));
$this->assertEquals("My life", $book->title);
$this->assertEquals('512ce86b98dee4a87a000000', $book->id);
$this->assertEquals(new MongoId('512ce86b98dee4a87a000000'), $book->_id);
Expand All @@ -34,7 +34,7 @@ public function testFindOneThrowInvalidArgument()
public function testFindOneOrFailThrowModelNotFound()
{
$this->setExpectedException('Mongovel\ModelNotFoundException');
Book::findOneOrFail('000000000000000000000000');
Book::findOneOrFail(new MongoId('000000000000000000000000'));
}

public function testCanFindAllDocuments()
Expand Down

0 comments on commit 42510c7

Please sign in to comment.