Skip to content

Commit

Permalink
relationships/hasMany: implemented getRawValue() method
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Apr 14, 2015
1 parent d85bc63 commit abbf351
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 13 deletions.
16 changes: 16 additions & 0 deletions src/Relationships/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,22 @@ public function isModified()
}


/**
* Returns primary values of enitities in relationship.
* @return mixed[]
*/
public function getRawValue()
{
$primaryValues = [];
foreach ($this->getIterator() as $entity) {
if ($entity->isPersisted()) {
$primaryValues[] = $entity->getValue('id');
}
}
return $primaryValues;
}


/**
* @return ICollection
*/
Expand Down
6 changes: 0 additions & 6 deletions src/Relationships/ManyHasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ public function persist($recursive = TRUE, & $queue = NULL)
}


public function getRawValue()
{
throw new NotImplementedException();
}


protected function createCollection()
{
if ($this->metadata->relationshipIsMain) {
Expand Down
6 changes: 0 additions & 6 deletions src/Relationships/OneHasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ public function persist($recursive = TRUE, & $queue = NULL)
}


public function getRawValue()
{
throw new NotImplementedException();
}


protected function createCollection()
{
$collection = $this->getTargetRepository()->getMapper()->createCollectionOneHasMany($this->metadata, $this->parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use Mockery;
use Nextras\Orm\Collection\ICollection;
use NextrasTests\Orm\Book;
use NextrasTests\Orm\DataTestCase;
use NextrasTests\Orm\Tag;
use Tester\Assert;

$dic = require_once __DIR__ . '/../../../bootstrap.php';
Expand Down Expand Up @@ -101,6 +102,26 @@ class RelationshipManyHasManyTest extends DataTestCase
}


public function testRawValue()
{
$book = $this->orm->books->getById(1);
Assert::same([1, 2], $book->tags->getRawValue());

$book->tags->remove(1);
Assert::same([2], $book->tags->getRawValue());

$tag = new Tag();
$tag->name = 'Test tag';
$tag->books->add($book);

Assert::same([2], $book->tags->getRawValue());

$this->orm->tags->persistAndFlush($tag);

Assert::same([2, 4], $book->tags->getRawValue());
}


public function testCaching()
{
$book = $this->orm->books->getById(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ class RelationshipOneHasManyTest extends DataTestCase
}


public function testRawValue()
{
$author = $this->orm->authors->getById(1);
Assert::same([2, 1], $author->books->getRawValue());

$this->orm->books->remove(1);
Assert::same([2], $author->books->getRawValue());

$book = new Book();
$book->author = $author;
$book->title = 'Test book';
$book->publisher = 1;

Assert::same([2], $author->books->getRawValue());

$this->orm->books->persistAndFlush($book);

Assert::same([5, 2], $author->books->getRawValue());
}


public function testPersistance()
{
$author1 = $this->e('NextrasTests\Orm\Author');
Expand Down
2 changes: 1 addition & 1 deletion tests/db/pgsql-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ INSERT INTO "tags" ("id", "name") VALUES (1, 'Tag 1');
INSERT INTO "tags" ("id", "name") VALUES (2, 'Tag 2');
INSERT INTO "tags" ("id", "name") VALUES (3, 'Tag 3');

SELECT setval('tags_id_seq', 4, TRUE);
SELECT setval('tags_id_seq', 3, TRUE);


INSERT INTO "books" ("id", "author_id", "translator_id", "title", "next_part", "publisher_id") VALUES (1, 1, 1, 'Book 1', NULL, 1);
Expand Down

0 comments on commit abbf351

Please sign in to comment.