Skip to content

Commit

Permalink
Added support for new GetLeftRelations protocol buffer method.
Browse files Browse the repository at this point in the history
From a remote model, treated as a right-hand entity, it is now possible to query back to retrieve a list of owning left-hand models
  • Loading branch information
Dan Richards committed Nov 17, 2017
1 parent b36fdfb commit 861a32a
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 18 deletions.
41 changes: 23 additions & 18 deletions protobuf/GPBMetadata/Relationship.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions protobuf/Relationship/RelationshipRightEntity.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions protobuf/Relationship/RelationshipServiceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ public function GetRelations(\Relationship\RelationshipLeftEntity $argument,
$metadata, $options);
}

/**
* Get the dominant left hand entity from a right hand entity.
* @param \Relationship\RelationshipRightEntity $argument input argument
* @param array $metadata metadata
* @param array $options call options
*/
public function GetLeftRelations(\Relationship\RelationshipRightEntity $argument,
$metadata = [], $options = []) {
return $this->_serverStreamRequest('/relationship.RelationshipService/GetLeftRelations',
$argument,
['\Relationship\Relationship', 'decode'],
$metadata, $options);
}

/**
* Update an existing relationship.
* @param \Relationship\UpdateRelationshipRequest $argument input argument
Expand Down
32 changes: 32 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use LushDigital\MicroServiceRemoteModels\Traits\TimeoutTrait;
use Psr\Http\Message\ResponseInterface;
use Relationship\RelationshipLeftEntity;
use Relationship\RelationshipRightEntity;
use Relationship\RelationshipServiceClient;

/**
Expand Down Expand Up @@ -533,4 +534,35 @@ protected function getGrpcPort()
{
return !empty(getenv('REMOTE_MODEL_GRPC_PORT')) ? getenv('REMOTE_MODEL_GRPC_PORT') : 50051;
}

/**
* Get a list of all remote models that own the current model.
*
* @param string $ownerModel
* Fully qualified class name for the owner model.
*
* @return Model[]
*/
public function getOwners($ownerModel)
{
$rightEntity = new RelationshipRightEntity;
$rightEntity->setRightId($this->model->getPrimaryKeyValue());

// Get all related IDs for this model.
$call = $this->getRelationshipClient((new $ownerModel), $this->getModel())
->GetLeftRelations($rightEntity, [], ['timeout' => $this->getTimeoutVal(10)]);
$owners = $call->responses();

$ownerInstances = [];

// Detach each relation instance that we find.
foreach ($owners as $index => $owner) {
$ownerInstance = new $ownerModel;
$ownerInstance->setPrimaryKeyValue($owner->getLeftId());

$ownerInstances[] = $ownerInstance;
}

return $ownerInstances;
}
}
12 changes: 12 additions & 0 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,4 +590,16 @@ public function getSavePromise()
]);
}
}

/**
* Get a list of all remote models that own this one.
*
* @param string $ownerModel
* Fully qualified class name for the owner model.
* @return Model[]
*/
public function getOwners($ownerModel)
{
return $this->newQueryBuilder()->getOwners($ownerModel);
}
}

0 comments on commit 861a32a

Please sign in to comment.