Skip to content

Commit

Permalink
entity: primary replaced by isMain in relationship modifiers (BC break!)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Dec 30, 2015
1 parent c85be03 commit 340b9f1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
14 changes: 7 additions & 7 deletions doc/relationships.texy
Expand Up @@ -6,15 +6,15 @@ Orm provides very efficient way to work with entity relationships. Orm recognize
- **1:m** - one has many: *author has many books*
- **m:1** - many has one: *book has one author*
- **m:n** - many has many: *book has many tags, tag is associated with many books*
- **1:1** - one has one: *user has one settings*, the reference for related entity is stored only on the side that is marked as a primary
- **1:1** - one has one: *user has one settings*, the reference for related entity is stored only on the side that is marked as main.

Use a relationship modifier to define relationship property. Modifiers require to define a target entity, some modifiers need to be defined on the both sides, then the reverse property definition is compulsory. If you want to define only one-sided relationship, use `oneSided=true` parameter. Other parameters are optional: ordering, setting a cascade, or making the current side primary (persisting is driven by the primary side). At least one side of `m:n` or `1:1d` has to be defined as the primary. Relationships do not support getters and setters as other entity properties.

/--code php
{1:1 EntityName::$reversePropertyName}
{m:1 EntityName::$reversePropertyName}
{1:m EntityName::$reversePropertyName, orderBy=property}
{m:n EntityName::$reversePropertyName, primary=true, orderBy=[property, DESC]}
{m:n EntityName::$reversePropertyName, isMain=true, orderBy=[property, DESC]}
\--

Cascade
Expand Down Expand Up @@ -103,7 +103,7 @@ use Nextras\Orm\Relationships\ManyHasMany;

/**
* @property int $id {primary}
* @property ManyHasMany|Tag[] $tags {m:n Tag::$books, primary=true}
* @property ManyHasMany|Tag[] $tags {m:n Tag::$books, isMain=true}
*/
class Book extends Nextras\Orm\Entity\Entity
{}
Expand All @@ -127,7 +127,7 @@ use Nextras\Orm\Relationships\ManyHasMany;

/**
* @property int $id {primary}
* @property ManyHasMany|Tag[] $tags {m:n Tag, primary=true, oneSided=true}
* @property ManyHasMany|Tag[] $tags {m:n Tag, isMain=true, oneSided=true}
*/
class Book extends Nextras\Orm\Entity\Entity
{}
Expand Down Expand Up @@ -158,7 +158,7 @@ Reference will be stored in `book.ean_id`.
/--code php
/**
* @property int $id {primary}
* @property EanCode $ean {1:1 Ean::$book, primary=true}
* @property EanCode $ean {1:1 Ean::$book, isMain=true}
*/
class Book extends Nextras\Orm\Entity\Entity
{}
Expand All @@ -180,7 +180,7 @@ Only the non-primary side is optional. Reference will be stored in `book.ean_id`
/--code php
/**
* @property int $id {primary}
* @property EanCode $ean {1:1 Ean, primary=true, oneSided=true}
* @property EanCode $ean {1:1 Ean, isMain=true, oneSided=true}
*/
class Book extends Nextras\Orm\Entity\Entity
{}
Expand All @@ -196,7 +196,7 @@ Reference will be stored in `book.next_volume_id`.
/--code php
/**
* @property int $id {primary}
* @property Book|NULL $nextVolume {1:1 Book::$previousVolume, primary=true}
* @property Book|NULL $nextVolume {1:1 Book::$previousVolume, isMain=true}
* @property Book|NULL $previousVolume {1:1 Book::$nextVolume}
*/
class Book extends Nextras\Orm\Entity\Entity
Expand Down
13 changes: 8 additions & 5 deletions src/Entity/Reflection/MetadataParser.php
Expand Up @@ -247,9 +247,9 @@ protected function parseOneHasOne(PropertyMetadata $property, array &$args)
$property->relationship = new PropertyRelationshipMetadata();
$property->relationship->type = PropertyRelationshipMetadata::ONE_HAS_ONE;
$property->container = OneHasOne::class;
$this->processRelationshipIsMain($args, $property);
$this->processRelationshipEntityProperty($args, $property);
$this->processRelationshipCascade($args, $property);
$this->processRelationshipPrimary($args, $property);
}


Expand Down Expand Up @@ -279,7 +279,7 @@ protected function parseManyHasMany(PropertyMetadata $property, array &$args)
$property->relationship = new PropertyRelationshipMetadata();
$property->relationship->type = PropertyRelationshipMetadata::MANY_HAS_MANY;
$property->container = ManyHasMany::class;
$this->processRelationshipPrimary($args, $property);
$this->processRelationshipIsMain($args, $property);
$this->processRelationshipEntityProperty($args, $property);
$this->processRelationshipCascade($args, $property);
$this->processRelationshipOrder($args, $property);
Expand Down Expand Up @@ -361,10 +361,13 @@ private function processRelationshipOrder(array &$args, PropertyMetadata $proper
}


private function processRelationshipPrimary(array &$args, PropertyMetadata $property)
private function processRelationshipIsMain(array &$args, PropertyMetadata $property)
{
$property->relationship->isMain = isset($args['primary']) && $args['primary'];
unset($args['primary']);
$property->relationship->isMain = (isset($args['primary']) && $args['primary']) || (isset($args['isMain']) && $args['isMain']);
if (isset($args['primary'])) {
trigger_error("Primary parameter of relationship modifier in {$this->currentReflection->name}::\${$property->name} property is deprecated. Use isMain parameter.", E_USER_DEPRECATED);
}
unset($args['primary'], $args['isMain']);
}


Expand Down
Expand Up @@ -22,11 +22,11 @@ $dic = require_once __DIR__ . '/../../../../bootstrap.php';
/**
* @property int $id {primary}
* @property mixed $test1 {m:m Foo::$property}
* @property mixed $test2 {m:m Foo::$property, primary=true}
* @property mixed $test2 {m:m Foo::$property, isMain=true}
* @property mixed $test3 {m:m Foo::$property, orderBy=this->entity->id}
* @property mixed $test4 {m:m Foo::$property, primary=true, orderBy=[id, DESC]}
* @property mixed $test4 {m:m Foo::$property, isMain=true, orderBy=[id, DESC]}
* @property mixed $test5 {m:m Foo::$property, orderBy=id}
* @property mixed $test6 {m:m Foo::$property, primary=true, orderBy=id}
* @property mixed $test6 {m:m Foo::$property, isMain=true, orderBy=id}
*/
class ManyHasManyTestEntity extends Entity
{}
Expand Down
6 changes: 3 additions & 3 deletions tests/inc/model/book/Book.php
Expand Up @@ -13,10 +13,10 @@
* @property string $title
* @property Author $author {m:1 Author::$books}
* @property Author|NULL $translator {m:1 Author::$translatedBooks}
* @property MHM|Tag[] $tags {m:n Tag::$books, primary=true}
* @property Book|NULL $nextPart {1:1 Book::$previousPart, primary=true}
* @property MHM|Tag[] $tags {m:n Tag::$books, isMain=true}
* @property Book|NULL $nextPart {1:1 Book::$previousPart, isMain=true}
* @property Book|NULL $previousPart {1:1 Book::$nextPart}
* @property Ean|NULL $ean {1:1 Ean::$book, primary=true, cascade=[persist, remove]}
* @property Ean|NULL $ean {1:1 Ean::$book, isMain=true, cascade=[persist, remove]}
* @property Publisher $publisher {m:1 Publisher::$books}
* @property DateTimeImmutable $publishedAt {default now}
* @property NULL|DateTime $printedAt
Expand Down

0 comments on commit 340b9f1

Please sign in to comment.