-
Notifications
You must be signed in to change notification settings - Fork 99
Added the NestedRelation #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alexberezinsky
wants to merge
5
commits into
spotorm:master
Choose a base branch
from
alexberezinsky:nested_relation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| <?php | ||
| namespace Spot; | ||
|
|
||
| use Spot\Relation\RelationAbstract; | ||
|
|
||
| /** | ||
| * Base DataMapper Interface | ||
| * | ||
|
|
||
This file contains hidden or 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,186 @@ | ||
| <?php | ||
| namespace Spot\Relation; | ||
|
|
||
| use Spot\EntityInterface; | ||
| use Spot\Entity\Collection; | ||
|
|
||
| /** | ||
| * NestedRelation | ||
| * | ||
| * Used for eager-loading multilevel relations | ||
| * | ||
| * @package Spot | ||
| */ | ||
| class NestedRelation extends RelationAbstract | ||
| { | ||
| /** | ||
| * @var RelationAbstract | ||
| */ | ||
| protected $relationObject; | ||
|
|
||
| /** | ||
| * @var RelationAbstract | ||
| */ | ||
| protected $parentRelationObject; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| protected $parentRelationName; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| protected $relationName; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| protected $entityName; | ||
|
|
||
| /* | ||
| * @var Collection | ||
| */ | ||
| protected $relationCollection; | ||
|
|
||
| /** | ||
| * @var array | ||
| */ | ||
| protected $relationCollectionReversedIdentities = []; | ||
|
|
||
| /** | ||
| * NestedRelation constructor | ||
| * | ||
| * @param RelationAbstract $relationObject | ||
| * @param RelationAbstract $parentRelationObject | ||
| */ | ||
| public function __construct ( | ||
| RelationAbstract $relationObject, | ||
| RelationAbstract $parentRelationObject | ||
| ) | ||
| { | ||
| $this->relationObject = $relationObject; | ||
| $this->parentRelationObject = $parentRelationObject; | ||
| $this->entityName = $relationObject->entityName(); | ||
| } | ||
|
|
||
| /** | ||
| * Set identity values from given collection | ||
| * | ||
| * @param \Spot\Entity\Collection | ||
| */ | ||
| public function identityValuesFromCollection(Collection $collection) | ||
| { | ||
| throw new \BadMethodCallException("This method is not implemented in NestedRelation class"); | ||
| } | ||
|
|
||
| /** | ||
| * Build query object | ||
| * | ||
| */ | ||
| protected function buildQuery() | ||
| { | ||
| throw new \BadMethodCallException("This method is not implemented in NestedRelation class"); | ||
| } | ||
|
|
||
| /** | ||
| * Map relation results to original collection of entities | ||
| * | ||
| * @param string Relation name | ||
| * @param \Spot\Entity\Collection Collection of original entities to map results of query back to | ||
| * | ||
| * @return \Spot\Entity\Collection | ||
| * | ||
| * @throws \Exception | ||
| */ | ||
| public function eagerLoadOnCollection($relationName, Collection $collection) | ||
| { | ||
| $relationNames = explode('.', $relationName); | ||
alexberezinsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $this->relationName = array_pop($relationNames); | ||
| $this->parentRelationName = array_pop($relationNames); | ||
|
|
||
| $this->createRelationCollection($collection); | ||
|
|
||
| $filledRelationCollection = $this->relationObject->eagerLoadOnCollection($this->relationName, $this->relationCollection); | ||
|
|
||
| if (!empty($this->relationCollectionReversedIdentities)) { | ||
| $result = []; | ||
| foreach ($filledRelationCollection as $ent) { | ||
| if (isset($this->relationCollectionReversedIdentities[$ent->primaryKey()])) { | ||
| $result[$this->relationCollectionReversedIdentities[$ent->primaryKey()]][] = $ent; | ||
| } | ||
| } | ||
| $filledRelationCollection = $result; | ||
| } | ||
|
|
||
| return $this->addFilledCollection($collection, $filledRelationCollection); | ||
| } | ||
|
|
||
| /** | ||
| * @param $collection | ||
| * @param $filledRelationCollection | ||
| * | ||
| * @return mixed | ||
| */ | ||
| public function addFilledCollection($collection, $filledRelationCollection) | ||
| { | ||
| $parentCollection = $collection; | ||
| if ($this->parentRelationObject instanceof NestedRelation) { | ||
| $parentCollection = $this->parentRelationObject->relationCollection; | ||
| } | ||
|
|
||
| foreach($parentCollection as $entity) { | ||
| if (isset($filledRelationCollection[$entity->primaryKey()])) { | ||
| $entity->relation($this->parentRelationName, $filledRelationCollection[$entity->primaryKey()]); | ||
| } | ||
| } | ||
|
|
||
| if ($this->parentRelationObject instanceof NestedRelation) { | ||
| return $this->parentRelationObject->addFilledCollection($collection, $parentCollection); | ||
| } | ||
|
|
||
| return $parentCollection; | ||
| } | ||
|
|
||
| /** | ||
| * @param Collection $collection | ||
| */ | ||
| public function createRelationCollection(Collection $collection) | ||
| { | ||
| if ($this->parentRelationObject instanceof NestedRelation) { | ||
| $collection = $this->parentRelationObject->relationCollection; | ||
| } | ||
| $relationCollection = []; | ||
| $resultsIdentities = []; | ||
| foreach($collection as $entity) { | ||
| $relatedEntity = $entity->relation($this->parentRelationName); | ||
| if ($relatedEntity instanceof Collection) { | ||
| foreach ($relatedEntity as $childEntity) { | ||
| $relationCollection[] = $childEntity; | ||
| $resultsIdentities[] = $childEntity->primaryKey(); | ||
| $this->relationCollectionReversedIdentities[$childEntity->primaryKey()] = $entity->primaryKey(); | ||
| } | ||
| } else { | ||
| $relationCollection[$entity->primaryKey()] = $relatedEntity; | ||
| $resultsIdentities[] = $relatedEntity->primaryKey(); | ||
| } | ||
| } | ||
| $this->relationCollection = new Collection($relationCollection, $resultsIdentities); | ||
| $this->relationObject->identityValuesFromCollection($this->relationCollection); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Save related entities | ||
| * | ||
| * @param EntityInterface $entity Entity to save relation from | ||
| * @param string $relationName Name of the relation to save | ||
| * @param array $options Options to pass to the mappers | ||
| * | ||
| * @throws \Exception | ||
| */ | ||
| public function save(EntityInterface $entity, $relationName, $options = []) | ||
| { | ||
| throw new \BadMethodCallException("This method is not implemented in NestedRelation class"); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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,37 @@ | ||
| <?php | ||
| namespace SpotTest\Entity\Post; | ||
|
|
||
| use DateTime; | ||
| use Spot\Entity; | ||
| use Spot\EntityInterface; | ||
| use Spot\MapperInterface; | ||
| use Spot\Query; | ||
|
|
||
| /** | ||
| * Post Comment | ||
| * | ||
| * @package Spot | ||
| */ | ||
| class UserComment extends Entity | ||
| { | ||
| protected static $table = 'test_post_user_comments'; | ||
|
|
||
| public static function fields() | ||
| { | ||
| return [ | ||
| 'id' => ['type' => 'integer', 'primary' => true, 'autoincrement' => true], | ||
| 'post_id' => ['type' => 'integer', 'index' => true, 'required' => true], | ||
| 'user_id' => ['type' => 'integer', 'index' => true, 'required' => true], | ||
| 'body' => ['type' => 'text', 'required' => true], | ||
| 'date_created' => ['type' => 'datetime'] | ||
| ]; | ||
| } | ||
|
|
||
| public static function relations(MapperInterface $mapper, EntityInterface $entity) | ||
| { | ||
| return [ | ||
| 'post' => $mapper->belongsTo($entity, 'SpotTest\Entity\Post', 'post_id'), | ||
| 'user' => $mapper->belongsTo($entity, 'SpotTest\Entity\User', 'user_id') | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or 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,27 @@ | ||
| <?php | ||
| namespace SpotTest\Entity; | ||
|
|
||
| /** | ||
| * Author | ||
| * | ||
| * @package Spot | ||
| */ | ||
| class User extends \Spot\Entity | ||
| { | ||
| protected static $table = 'test_users'; | ||
|
|
||
| public static function fields() | ||
| { | ||
| return [ | ||
| 'id' => ['type' => 'integer', 'primary' => true, 'autoincrement' => true], | ||
| 'email' => ['type' => 'string', 'required' => true, 'unique' => true, | ||
| 'validation' => [ | ||
| 'email', | ||
| 'length' => [4, 255] | ||
| ] | ||
| ], // Unique | ||
| 'password' => ['type' => 'text', 'required' => true], | ||
| 'date_created' => ['type' => 'datetime', 'value' => new \DateTime()] | ||
| ]; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.