From 861a32a4a041a345697ea40cc986485c8d8b33fa Mon Sep 17 00:00:00 2001 From: Dan Richards Date: Fri, 17 Nov 2017 13:23:57 +0000 Subject: [PATCH] Added support for new GetLeftRelations protocol buffer method. 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 --- protobuf/GPBMetadata/Relationship.php | 41 +++++++++------- .../Relationship/RelationshipRightEntity.php | 48 +++++++++++++++++++ .../RelationshipServiceClient.php | 14 ++++++ src/Builder.php | 32 +++++++++++++ src/Model.php | 12 +++++ 5 files changed, 129 insertions(+), 18 deletions(-) create mode 100644 protobuf/Relationship/RelationshipRightEntity.php diff --git a/protobuf/GPBMetadata/Relationship.php b/protobuf/GPBMetadata/Relationship.php index f52de40..4f1502b 100644 --- a/protobuf/GPBMetadata/Relationship.php +++ b/protobuf/GPBMetadata/Relationship.php @@ -15,29 +15,34 @@ public static function initOnce() { return; } $pool->internalAddGeneratedFile(hex2bin( - "0aa4050a1272656c6174696f6e736869702e70726f746f120c72656c6174" . + "0aab060a1272656c6174696f6e736869702e70726f746f120c72656c6174" . "696f6e73686970222f0a0c52656c6174696f6e73686970120e0a066c6566" . "744964180120012805120f0a077269676874496418022001280522480a14" . "52656c6174696f6e73686970526573706f6e7365120e0a066c6566744964" . "180120012805120f0a0772696768744964180220012805120f0a07737563" . "6365737318032001280822280a1652656c6174696f6e736869704c656674" . - "456e74697479120e0a066c656674496418012001280522500a1955706461" . - "746552656c6174696f6e7368697052657175657374120e0a066c65667449" . - "64180120012805120f0a077269676874496418022001280512120a0a6f6c" . - "64526967687449641803200128053280030a1352656c6174696f6e736869" . - "705365727669636512560a1243726561746552656c6174696f6e73686970" . - "121a2e72656c6174696f6e736869702e52656c6174696f6e736869701a22" . - "2e72656c6174696f6e736869702e52656c6174696f6e7368697052657370" . - "6f6e7365220012540a0c47657452656c6174696f6e7312242e72656c6174" . - "696f6e736869702e52656c6174696f6e736869704c656674456e74697479" . - "1a1a2e72656c6174696f6e736869702e52656c6174696f6e736869702200" . - "300112630a1255706461746552656c6174696f6e7368697012272e72656c" . - "6174696f6e736869702e55706461746552656c6174696f6e736869705265" . - "71756573741a222e72656c6174696f6e736869702e52656c6174696f6e73" . - "686970526573706f6e7365220012560a1244656c65746552656c6174696f" . - "6e73686970121a2e72656c6174696f6e736869702e52656c6174696f6e73" . - "6869701a222e72656c6174696f6e736869702e52656c6174696f6e736869" . - "70526573706f6e73652200620670726f746f33" + "456e74697479120e0a066c6566744964180120012805222a0a1752656c61" . + "74696f6e736869705269676874456e74697479120f0a0772696768744964" . + "18012001280522500a1955706461746552656c6174696f6e736869705265" . + "7175657374120e0a066c6566744964180120012805120f0a077269676874" . + "496418022001280512120a0a6f6c645269676874496418032001280532db" . + "030a1352656c6174696f6e736869705365727669636512560a1243726561" . + "746552656c6174696f6e73686970121a2e72656c6174696f6e736869702e" . + "52656c6174696f6e736869701a222e72656c6174696f6e736869702e5265" . + "6c6174696f6e73686970526573706f6e7365220012540a0c47657452656c" . + "6174696f6e7312242e72656c6174696f6e736869702e52656c6174696f6e" . + "736869704c656674456e746974791a1a2e72656c6174696f6e736869702e" . + "52656c6174696f6e736869702200300112590a104765744c65667452656c" . + "6174696f6e7312252e72656c6174696f6e736869702e52656c6174696f6e" . + "736869705269676874456e746974791a1a2e72656c6174696f6e73686970" . + "2e52656c6174696f6e736869702200300112630a1255706461746552656c" . + "6174696f6e7368697012272e72656c6174696f6e736869702e5570646174" . + "6552656c6174696f6e73686970526571756573741a222e72656c6174696f" . + "6e736869702e52656c6174696f6e73686970526573706f6e736522001256" . + "0a1244656c65746552656c6174696f6e73686970121a2e72656c6174696f" . + "6e736869702e52656c6174696f6e736869701a222e72656c6174696f6e73" . + "6869702e52656c6174696f6e73686970526573706f6e7365220062067072" . + "6f746f33" )); static::$is_initialized = true; diff --git a/protobuf/Relationship/RelationshipRightEntity.php b/protobuf/Relationship/RelationshipRightEntity.php new file mode 100644 index 0000000..084bc69 --- /dev/null +++ b/protobuf/Relationship/RelationshipRightEntity.php @@ -0,0 +1,48 @@ + + * Message representing the right hand (dominant) entity. + * + * + * Protobuf type relationship.RelationshipRightEntity + */ +class RelationshipRightEntity extends \Google\Protobuf\Internal\Message +{ + /** + * int32 rightId = 1; + */ + private $rightId = 0; + + public function __construct() { + \GPBMetadata\Relationship::initOnce(); + parent::__construct(); + } + + /** + * int32 rightId = 1; + */ + public function getRightId() + { + return $this->rightId; + } + + /** + * int32 rightId = 1; + */ + public function setRightId($var) + { + GPBUtil::checkInt32($var); + $this->rightId = $var; + } + +} + diff --git a/protobuf/Relationship/RelationshipServiceClient.php b/protobuf/Relationship/RelationshipServiceClient.php index 48210ce..9f32ae7 100644 --- a/protobuf/Relationship/RelationshipServiceClient.php +++ b/protobuf/Relationship/RelationshipServiceClient.php @@ -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 diff --git a/src/Builder.php b/src/Builder.php index 0278300..ce2470f 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -13,6 +13,7 @@ use LushDigital\MicroServiceRemoteModels\Traits\TimeoutTrait; use Psr\Http\Message\ResponseInterface; use Relationship\RelationshipLeftEntity; +use Relationship\RelationshipRightEntity; use Relationship\RelationshipServiceClient; /** @@ -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; + } } \ No newline at end of file diff --git a/src/Model.php b/src/Model.php index 839267a..04855a5 100644 --- a/src/Model.php +++ b/src/Model.php @@ -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); + } } \ No newline at end of file