-
-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store redundant fields in references
- Loading branch information
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
tests/Doctrine/ODM/MongoDB/Tests/Functional/RedundantFieldsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
namespace Doctrine\ODM\MongoDB\Tests\Functional; | ||
|
||
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||
|
||
class RedundantFieldsTest extends \Doctrine\ODM\MongoDB\Tests\BaseTest | ||
{ | ||
public function testSimpleRedundantField() | ||
{ | ||
$blog = new Blog('my-blog'); | ||
$this->dm->persist($blog); | ||
|
||
$post = new Post($blog, 'My first post'); | ||
$this->dm->persist($post); | ||
$this->dm->flush(); | ||
|
||
$rawPost = $this->dm->createQueryBuilder(Post::class) | ||
->find() | ||
->field('id') | ||
->equals($post->getId()) | ||
->hydrate(false) | ||
->getQuery() | ||
->getSingleResult(); | ||
|
||
$this->assertSame('my-blog', $rawPost['blog']['title']); | ||
$this->assertInstanceOf(\MongoDate::class, $rawPost['blog']['creationDate']); | ||
} | ||
|
||
public function testDatabaseValueIsPreparedInQuery() | ||
{ | ||
$blog = new Blog('my-blog'); | ||
$this->dm->persist($blog); | ||
|
||
$post = new Post($blog, 'My first post'); | ||
$this->dm->persist($post); | ||
$this->dm->flush(); | ||
|
||
$result = $this->dm->createQueryBuilder(Post::class) | ||
->find() | ||
->field('blog.creationDate') | ||
->equals($blog->getCreationDate()) | ||
->getQuery() | ||
->execute(); | ||
|
||
$this->assertCount(1, $result); | ||
} | ||
} | ||
|
||
/** | ||
* @ODM\Document() | ||
*/ | ||
class Blog | ||
{ | ||
/** @ODM\Id */ | ||
protected $id; | ||
|
||
/** @ODM\Field(type="string") */ | ||
protected $title; | ||
|
||
/** @ODM\Field(type="date") */ | ||
protected $creationDate; | ||
|
||
public function __construct($title) | ||
{ | ||
$this->title = $title; | ||
$this->creationDate = new \DateTime(); | ||
} | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getTitle() | ||
{ | ||
return $this->title; | ||
} | ||
|
||
public function getCreationDate() | ||
{ | ||
return $this->creationDate; | ||
} | ||
} | ||
|
||
/** @ODM\Document */ | ||
class Post | ||
{ | ||
/** @ODM\Id */ | ||
protected $id; | ||
|
||
/** @ODM\ReferenceOne(targetDocument="Blog", redundantFields={"title","creationDate"}) */ | ||
protected $blog; | ||
|
||
/** @ODM\Field(type="string") */ | ||
protected $title; | ||
|
||
public function __construct(Blog $blog, $title) | ||
{ | ||
$this->blog = $blog; | ||
$this->title = $title; | ||
} | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getBlog() | ||
{ | ||
return $this->blog; | ||
} | ||
|
||
public function getTitle() | ||
{ | ||
return $this->title; | ||
} | ||
} |