Skip to content

Commit

Permalink
relationships: allow connecting unattached entities [closes #79]
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Apr 27, 2015
1 parent 49e9723 commit 7dc4d93
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
4 changes: 0 additions & 4 deletions doc/default.texy
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ $publisher->name = '7K publisher';

$book = new Book();
$book->title = 'My Life on The Wall';


$orm->books->attach($book); // this allows to connect entities together

$book->author = $author;
$book->publisher = $publisher;

Expand Down
5 changes: 2 additions & 3 deletions doc/entity.texy
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,19 @@ $member->isPersisted(); // FALSE
$member->isAttached(); // FALSE
\--

Attaching entities to the repository is letting Orm know about your entities, it does not store the entity. If you are connecting entities, at least one of them has to be attached to its repository. Attaching to repository injects required dependencies into your entity (by inject property annotations or inject methods). If you need some dependency before attaching entity to the repository, feel free to pass the dependency through the constructor, which is by default empty.
Attaching entities to the repository is letting Orm know about your entities, it does not store the entity. Attaching to repository injects required dependencies into your entity (by inject property annotations or inject methods). If you need some dependency before attaching entity to the repository, feel free to pass the dependency through the constructor, which is by default empty.

Each entity can be created "manually". Entities can be simply connected together. Let's see an example:

/--php
$author = new Author();
$orm->authors->attach($author);

$book = new Book();
$book->author = $author;
$book->tags->set([new Tag(), new Tag()]);
\--

In the example above, all newly created entities are automatically attached to their repositories. See more in [relationships chapter | relationships].
If book was attached to repository, all other newly created entities would be automatically attached to their repositories too. See more in [relationships chapter | relationships].

-----------

Expand Down
3 changes: 0 additions & 3 deletions src/Relationships/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,6 @@ protected function createEntity($entity, $need = TRUE)
} elseif ($model = $this->parent->getModel(FALSE)) {
$repository = $model->getRepositoryForEntity($entity);
$repository->attach($entity);

} else {
throw new InvalidStateException('At least one entity has to be attached to IRepository.');
}

return $entity;
Expand Down
59 changes: 59 additions & 0 deletions tests/cases/integration/Relationships/entity.relationships.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* @testCase
* @dataProvider ../../../sections.ini
*/

namespace NextrasTests\Orm\Integration\Relationships;

use Mockery;
use NextrasTests\Orm\Author;
use NextrasTests\Orm\Book;
use NextrasTests\Orm\DataTestCase;
use NextrasTests\Orm\Publisher;
use NextrasTests\Orm\Tag;
use Tester\Assert;

$dic = require_once __DIR__ . '/../../../bootstrap.php';


class EntityRelationshipsTest extends DataTestCase
{

public function testBasics()
{
$author = new Author();
$author->name = 'Jon Snow';

$publisher = new Publisher();
$publisher->name = '7K';

$book = new Book();
$book->title = 'A new book';
$book->author = $author;
$book->publisher = $publisher;
$book->tags->set([new Tag('Awesome')]);

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

Assert::true($author->isAttached());
Assert::true($author->isPersisted());
Assert::false($author->isModified());
Assert::same(3, $author->id);

Assert::true($book->isAttached());
Assert::true($book->isPersisted());
Assert::false($book->isModified());
Assert::same(5, $book->id);

Assert::same(1, $book->tags->count());
Assert::same(1, $book->tags->countStored());
Assert::same('Awesome', $book->tags->get()->fetch()->name);
}

}


$test = new EntityRelationshipsTest($dic);
$test->run();

0 comments on commit 7dc4d93

Please sign in to comment.