From 075584bc4ea8baf80aa729f0b8e793da7d7c2c80 Mon Sep 17 00:00:00 2001 From: alphadevx Date: Sun, 18 Mar 2018 16:33:06 +0000 Subject: [PATCH] #75 - merged the Relation::getRelatedObject() and Relation::getRelatedObjects() into a single Relation::getRelated() method --- Alpha/Controller/ArticleController.php | 2 +- Alpha/Controller/Controller.php | 2 +- Alpha/Controller/SearchController.php | 2 +- Alpha/Controller/TagController.php | 4 +- Alpha/Model/ActiveRecord.php | 4 +- Alpha/Model/Article.php | 4 +- Alpha/Model/Type/Relation.php | 77 ++++++++----------- Alpha/Util/Search/SearchProviderTags.php | 4 +- Alpha/View/ArticleView.php | 2 +- Alpha/View/Widget/RecordSelector.php | 2 +- test/Alpha/Test/Controller/ControllerTest.php | 2 +- .../Test/Controller/TagControllerTest.php | 10 +-- test/Alpha/Test/Model/TagTest.php | 4 +- test/Alpha/Test/Model/Type/RelationTest.php | 24 ++---- .../Util/Search/SearchProviderTagsTest.php | 6 +- 15 files changed, 66 insertions(+), 83 deletions(-) diff --git a/Alpha/Controller/ArticleController.php b/Alpha/Controller/ArticleController.php index d36df63f..f840c196 100644 --- a/Alpha/Controller/ArticleController.php +++ b/Alpha/Controller/ArticleController.php @@ -681,7 +681,7 @@ private function renderTags() $html = ''; if ($relation instanceof Relation) { - $tags = $relation->getRelatedObjects(); + $tags = $relation->getRelated(); if (count($tags) > 0) { $html .= '

Tags:'; diff --git a/Alpha/Controller/Controller.php b/Alpha/Controller/Controller.php index 14ab2b02..9fe7882e 100644 --- a/Alpha/Controller/Controller.php +++ b/Alpha/Controller/Controller.php @@ -323,7 +323,7 @@ public function setRecord($record) // if the record has tags, use these as the meta keywords for this controller if ($this->record->isTagged()) { - $tags = $this->record->getPropObject('tags')->getRelatedObjects(); + $tags = $this->record->getPropObject('tags')->getRelated(); $keywords = ''; diff --git a/Alpha/Controller/SearchController.php b/Alpha/Controller/SearchController.php index 41059f04..e391c17a 100644 --- a/Alpha/Controller/SearchController.php +++ b/Alpha/Controller/SearchController.php @@ -209,7 +209,7 @@ protected function renderResultList($results, $query = '', $showTags = true) $body .= $view->listView(array('formAction' => $URI)); if ($showTags) { - $tags = $bo->getPropObject('tags')->getRelatedObjects(); + $tags = $bo->getPropObject('tags')->getRelated(); if (count($tags) > 0) { $body .= '

Tags: '; diff --git a/Alpha/Controller/TagController.php b/Alpha/Controller/TagController.php index cfc8657e..9b2fa3c7 100644 --- a/Alpha/Controller/TagController.php +++ b/Alpha/Controller/TagController.php @@ -199,7 +199,7 @@ public function doGET($request) try { $record->load($ActiveRecordID); - $tags = $record->getPropObject('tags')->getRelatedObjects(); + $tags = $record->getPropObject('tags')->getRelated(); ActiveRecord::disconnect(); @@ -340,7 +340,7 @@ public function doPOST($request) try { $record->load($ActiveRecordID); - $tags = $record->getPropObject('tags')->getRelatedObjects(); + $tags = $record->getPropObject('tags')->getRelated(); ActiveRecord::begin(); diff --git a/Alpha/Model/ActiveRecord.php b/Alpha/Model/ActiveRecord.php index 0ca5c35e..5950fc5c 100644 --- a/Alpha/Model/ActiveRecord.php +++ b/Alpha/Model/ActiveRecord.php @@ -759,7 +759,7 @@ public function delete() // should set related field values to null (MySQL is doing this for us as-is) if ($prop->getRelationType() == 'ONE-TO-MANY' && !$prop->getRelatedClass() == 'Alpha\Model\Tag') { - $relatedObjects = $prop->getRelatedObjects(); + $relatedObjects = $prop->getRelated(); foreach ($relatedObjects as $object) { $object->set($prop->getRelatedClassField(), null); @@ -771,7 +771,7 @@ public function delete() if ($prop->getRelationType() == 'ONE-TO-MANY' && $prop->getRelatedClass() == 'Alpha\Model\Tag') { // just making sure that the Relation is set to current ID as its transient $prop->setValue($this->getID()); - $relatedObjects = $prop->getRelatedObjects(); + $relatedObjects = $prop->getRelated(); foreach ($relatedObjects as $object) { $object->delete(); diff --git a/Alpha/Model/Article.php b/Alpha/Model/Article.php index 32835bf5..9ab30292 100644 --- a/Alpha/Model/Article.php +++ b/Alpha/Model/Article.php @@ -451,7 +451,7 @@ public function getArticleScore() */ public function getArticleVotes() { - $votes = $this->votes->getRelatedObjects(); + $votes = $this->votes->getRelated(); return $votes; } @@ -505,7 +505,7 @@ public function checkUserVoted() */ public function getArticleComments() { - $comments = $this->comments->getRelatedObjects(); + $comments = $this->comments->getRelated(); return $comments; } diff --git a/Alpha/Model/Type/Relation.php b/Alpha/Model/Type/Relation.php index e71ed13b..422cf221 100644 --- a/Alpha/Model/Type/Relation.php +++ b/Alpha/Model/Type/Relation.php @@ -616,88 +616,79 @@ public function getRelatedClassDisplayFieldValue($accessingClassName = '') } /** - * For one-to-many and many-to-many relations, get the objects on the other side. + * For one-to-many and many-to-many relations, get an array of records on the other side. For one-to-one + * relations, get the record (Alpha\Model\ActiveRecord instance) on the other side. * * string $accessingClassName Used to indicate the reading side when accessing from MANY-TO-MANY relation (leave blank for other relation types) * - * @return array + * @return array|Alpha\Model\ActiveRecord * - * @since 1.0 + * @since 3.0 * * @throws \Alpha\Exception\IllegalArguementException */ - public function getRelatedObjects($accessingClassName = '') + public function getRelated($accessingClassName = '') { + if ($this->relationType == 'ONE-TO-ONE') { + if (!class_exists($this->relatedClass)) { + throw new IllegalArguementException('Could not load the definition for the ActiveRecord class ['.$this->relatedClass.']'); + } + + $record = new $this->relatedClass(); + $record->loadByAttribute($this->getRelatedClassField(), $this->getValue()); + + return $record; + } + if ($this->relationType == 'ONE-TO-MANY') { if ($this->getValue() == '') { // if the value is empty, then return an empty array return array(); } - $obj = new $this->relatedClass(); + $record = new $this->relatedClass(); if ($this->relatedClass == 'Alpha\Model\Tag') { - $objects = $obj->loadTags($this->taggedClass, $this->getValue()); + $records = $record->loadTags($this->taggedClass, $this->getValue()); } else { - $objects = $obj->loadAllByAttribute($this->getRelatedClassField(), $this->getValue()); + $records = $record->loadAllByAttribute($this->getRelatedClassField(), $this->getValue()); } - return $objects; + return $records; } else { // MANY-TO-MANY if (empty($this->lookup)) { throw new IllegalArguementException('Tried to load related MANY-TO-MANY objects but no RelationLookup set on the Relation object!'); } if (empty($accessingClassName)) { - throw new IllegalArguementException('Tried to load related MANY-TO-MANY objects but no accessingClassName parameter set on the call to getRelatedObjects!'); + throw new IllegalArguementException('Tried to load related MANY-TO-MANY objects but no accessingClassName parameter set on the call to getRelated!'); } - $objects = array(); + $records = array(); // load objects on the right from accessing on the left if ($accessingClassName == $this->relatedClassLeft) { - $lookupObjects = $this->lookup->loadAllByAttribute('leftID', $this->value); + $lookups = $this->lookup->loadAllByAttribute('leftID', $this->value); - foreach ($lookupObjects as $lookupObject) { - $obj = new $this->relatedClassRight(); - $obj->load($lookupObject->get('rightID')); - array_push($objects, $obj); + foreach ($lookups as $lookup) { + $record = new $this->relatedClassRight(); + $record->load($lookup->get('rightID')); + array_push($records, $record); } } // load objects on the left from accessing on the right - if ($accessingClassName == $this->relatedClassRight && count($objects) == 0) { - $lookupObjects = $this->lookup->loadAllByAttribute('rightID', $this->value); + if ($accessingClassName == $this->relatedClassRight && count($records) == 0) { + $lookups = $this->lookup->loadAllByAttribute('rightID', $this->value); - foreach ($lookupObjects as $lookupObject) { - $obj = new $this->relatedClassLeft(); - $obj->load($lookupObject->get('leftID')); - array_push($objects, $obj); + foreach ($lookups as $lookup) { + $record = new $this->relatedClassLeft(); + $record->load($lookup->get('leftID')); + array_push($records, $record); } } - return $objects; + return $records; } } - /** - * For one-to-one relations, get the object on the other side. - * - * @return array - * - * @since 1.0 - * - * @throws \Alpha\Model\Type\IllegalArguementException - */ - public function getRelatedObject() - { - if (!class_exists($this->relatedClass)) { - throw new IllegalArguementException('Could not load the definition for the Record class ['.$this->relatedClass.']'); - } - - $obj = new $this->relatedClass(); - $obj->loadByAttribute($this->getRelatedClassField(), $this->getValue()); - - return $obj; - } - /** * Get the allowable size of the Relation in the database field. * diff --git a/Alpha/Util/Search/SearchProviderTags.php b/Alpha/Util/Search/SearchProviderTags.php index 370f1250..74ca450d 100644 --- a/Alpha/Util/Search/SearchProviderTags.php +++ b/Alpha/Util/Search/SearchProviderTags.php @@ -183,7 +183,7 @@ public function getRelated($sourceObject, $returnType = 'all', $start = 0, $limi if (count($matches) == 0) { // all the tags on the source object for comparison - $tags = $sourceObject->getPropObject('tags')->getRelatedObjects(); + $tags = $sourceObject->getPropObject('tags')->getRelated(); foreach ($tags as $tag) { $Tag = new Tag(); @@ -291,7 +291,7 @@ public function index($sourceObject) */ public function delete($sourceObject) { - $tags = $sourceObject->getPropObject('tags')->getRelatedObjects(); + $tags = $sourceObject->getPropObject('tags')->getRelated(); foreach ($tags as $tag) { $tag->delete(); diff --git a/Alpha/View/ArticleView.php b/Alpha/View/ArticleView.php index 1f92203d..176de5ba 100644 --- a/Alpha/View/ArticleView.php +++ b/Alpha/View/ArticleView.php @@ -199,7 +199,7 @@ public function editView($fields = array()) $tags = array(); if (is_object($this->record->getPropObject('tags'))) { - $tags = $this->record->getPropObject('tags')->getRelatedObjects(); + $tags = $this->record->getPropObject('tags')->getRelated(); } if (count($tags) > 0) { diff --git a/Alpha/View/Widget/RecordSelector.php b/Alpha/View/Widget/RecordSelector.php index 5ce4274a..ecc71785 100644 --- a/Alpha/View/Widget/RecordSelector.php +++ b/Alpha/View/Widget/RecordSelector.php @@ -209,7 +209,7 @@ public function render($expanded = false, $buttons = true) // render read-only list for one-to-many relations if ($this->relationObject->getRelationType() == 'ONE-TO-MANY') { - $objects = $this->relationObject->getRelatedObjects(); + $objects = $this->relationObject->getRelated(); if (count($objects) > 0) { // render tags differently diff --git a/test/Alpha/Test/Controller/ControllerTest.php b/test/Alpha/Test/Controller/ControllerTest.php index b44f6538..9b342339 100644 --- a/test/Alpha/Test/Controller/ControllerTest.php +++ b/test/Alpha/Test/Controller/ControllerTest.php @@ -601,7 +601,7 @@ public function testTagsMapToMetaKeywords() ActiveRecord::begin(); $this->article->save(); ActiveRecord::commit(); - $tags = $this->article->getPropObject('tags')->getRelatedObjects(); + $tags = $this->article->getPropObject('tags')->getRelated(); $found = false; foreach ($tags as $tag) { diff --git a/test/Alpha/Test/Controller/TagControllerTest.php b/test/Alpha/Test/Controller/TagControllerTest.php index 7ad0759c..18d29f65 100644 --- a/test/Alpha/Test/Controller/TagControllerTest.php +++ b/test/Alpha/Test/Controller/TagControllerTest.php @@ -134,7 +134,7 @@ public function testDoPOST() $article = $this->createArticle('testing'); $article->save(); - $tags = $article->getPropObject('tags')->getRelatedObjects(); + $tags = $article->getPropObject('tags')->getRelated(); $existingTags = array(); foreach ($tags as $tag) { @@ -150,7 +150,7 @@ public function testDoPOST() $this->assertEquals(200, $response->getStatus(), 'Testing the doPOST method'); - $tags = $article->getPropObject('tags')->getRelatedObjects(); + $tags = $article->getPropObject('tags')->getRelated(); $found = false; $tagID = ''; @@ -183,7 +183,7 @@ public function testDoDELETE() $article = $this->createArticle('testing'); $article->save(); - $tags = $article->getPropObject('tags')->getRelatedObjects(); + $tags = $article->getPropObject('tags')->getRelated(); $existingTags = array(); foreach ($tags as $tag) { @@ -197,7 +197,7 @@ public function testDoDELETE() $response = $front->process($request); - $tags = $article->getPropObject('tags')->getRelatedObjects(); + $tags = $article->getPropObject('tags')->getRelated(); $found = false; $tagID = ''; @@ -221,7 +221,7 @@ public function testDoDELETE() $this->assertEquals(301, $response->getStatus(), 'Testing the doDELETE method'); $this->assertTrue(strpos($response->getHeader('Location'), '/tag/'.urlencode('Alpha\Model\Article').'/'.$article->getID()) !== false, 'Testing the doDELETE method'); - $tags = $article->getPropObject('tags')->getRelatedObjects(); + $tags = $article->getPropObject('tags')->getRelated(); $notFound = true; diff --git a/test/Alpha/Test/Model/TagTest.php b/test/Alpha/Test/Model/TagTest.php index ba3b6498..5a6fd578 100644 --- a/test/Alpha/Test/Model/TagTest.php +++ b/test/Alpha/Test/Model/TagTest.php @@ -187,7 +187,7 @@ public function testTokenizeNoDuplicates() public function testSaveArticleGeneratesDescriptionTags() { $this->article->save(); - $tags = $this->article->getPropObject('tags')->getRelatedObjects(); + $tags = $this->article->getPropObject('tags')->getRelated(); $found = false; foreach ($tags as $tag) { @@ -207,7 +207,7 @@ public function testSaveArticleGeneratesDescriptionTags() public function testLoadTags() { $this->article->save(); - $tagsA = $this->article->getPropObject('tags')->getRelatedObjects(); + $tagsA = $this->article->getPropObject('tags')->getRelated(); $tag = new Tag(); $tagsB = $tag->loadTags('Alpha\Model\Article', $this->article->getID()); diff --git a/test/Alpha/Test/Model/Type/RelationTest.php b/test/Alpha/Test/Model/Type/RelationTest.php index 8eabf636..687e8574 100644 --- a/test/Alpha/Test/Model/Type/RelationTest.php +++ b/test/Alpha/Test/Model/Type/RelationTest.php @@ -321,7 +321,7 @@ public function testGetRelatedClassDisplayFieldValuePass() $person2->getPropObject('rights')->setValue($group->getID()); - $this->assertEquals(2, count($group->getPropObject('members')->getRelatedObjects('Alpha\Model\Rights')), 'testing the getRelatedClassDisplayFieldValue() method on MANY-TO-MANY relation'); + $this->assertEquals(2, count($group->getPropObject('members')->getRelated('Alpha\Model\Rights')), 'testing the getRelatedClassDisplayFieldValue() method on MANY-TO-MANY relation'); try { $this->assertEquals('user1@test.com,user2@test.com', $person2->getPropObject('rights')->getRelatedClassDisplayFieldValue(), 'testing the getRelatedClassDisplayFieldValue() method on MANY-TO-MANY relation'); @@ -407,11 +407,11 @@ public function testGetSideFail() } /** - * Testing the getRelatedObject method. + * Testing the getRelated method. * * @since 1.2.1 */ - public function testGetRelatedObject() + public function testGetRelated() { $oneToOneRel = new Relation(); $oneToOneRel->setRelatedClass('Alpha\Model\Person'); @@ -420,16 +420,8 @@ public function testGetRelatedObject() $oneToOneRel->setRelationType('ONE-TO-ONE'); $oneToOneRel->setValue($this->person->getID()); - $this->assertEquals($this->person->getUsername(), $oneToOneRel->getRelatedObject()->get('username'), 'testing the getRelatedObject method'); - } + $this->assertEquals($this->person->getUsername(), $oneToOneRel->getRelated()->get('username'), 'testing the getRelated method'); - /** - * Testing the getRelatedObjects method with a ONE-TO-MANY and MANY-TO-MANY relation. - * - * @since 1.2.1 - */ - public function testGetRelatedObjects() - { $group = new Rights(); $group->set('name', 'unittestgroup'); $group->save(); @@ -454,8 +446,8 @@ public function testGetRelatedObjects() $person2->getPropObject('rights')->setValue($group->getID()); - $this->assertEquals(2, count($group->getPropObject('members')->getRelatedObjects('Alpha\Model\Rights')), 'testing the getRelatedObjects method with a MANY-TO-MANY relation'); - $this->assertTrue($group->getPropObject('members')->getRelatedObjects('Alpha\Model\Rights')[0] instanceof Person, 'testing the getRelatedObjects method with a MANY-TO-MANY relation'); + $this->assertEquals(2, count($group->getPropObject('members')->getRelated('Alpha\Model\Rights')), 'testing the getRelated method with a MANY-TO-MANY relation'); + $this->assertTrue($group->getPropObject('members')->getRelated('Alpha\Model\Rights')[0] instanceof Person, 'testing the getRelated method with a MANY-TO-MANY relation'); $article = new Article(); $article->set('title', 'unit test'); @@ -474,7 +466,7 @@ public function testGetRelatedObjects() $comment2->getPropObject('articleID')->setValue($article->getID()); $comment2->save(); - $this->assertEquals(2, count($article->getPropObject('comments')->getRelatedObjects()), 'testing the getRelatedObjects method with a ONE-TO-MANY relation'); - $this->assertTrue($article->getPropObject('comments')->getRelatedObjects()[0] instanceof ArticleComment, 'testing the getRelatedObjects method with a ONE-TO-MANY relation'); + $this->assertEquals(2, count($article->getPropObject('comments')->getRelated()), 'testing the getRelated method with a ONE-TO-MANY relation'); + $this->assertTrue($article->getPropObject('comments')->getRelated()[0] instanceof ArticleComment, 'testing the getRelated method with a ONE-TO-MANY relation'); } } diff --git a/test/Alpha/Test/Util/Search/SearchProviderTagsTest.php b/test/Alpha/Test/Util/Search/SearchProviderTagsTest.php index 5eb9bb7a..f2c0e22a 100644 --- a/test/Alpha/Test/Util/Search/SearchProviderTagsTest.php +++ b/test/Alpha/Test/Util/Search/SearchProviderTagsTest.php @@ -138,7 +138,7 @@ public function testIndex() $provider = ServiceFactory::getInstance('Alpha\Util\Search\SearchProviderTags', 'Alpha\Util\Search\SearchProviderInterface'); $provider->index($this->article); - $tags = $this->article->getPropObject('tags')->getRelatedObjects(); + $tags = $this->article->getPropObject('tags')->getRelated(); $found = false; foreach ($tags as $tag) { @@ -158,14 +158,14 @@ public function testIndex() public function testDelete() { $this->article->save(); - $tags = $this->article->getPropObject('tags')->getRelatedObjects(); + $tags = $this->article->getPropObject('tags')->getRelated(); $this->assertTrue(count($tags) > 0, 'Confirming that tags exist after saving the article (ArticleObject::after_save_callback())'); $provider = ServiceFactory::getInstance('Alpha\Util\Search\SearchProviderTags', 'Alpha\Util\Search\SearchProviderInterface'); $provider->delete($this->article); - $tags = $this->article->getPropObject('tags')->getRelatedObjects(); + $tags = $this->article->getPropObject('tags')->getRelated(); $this->assertTrue(count($tags) == 0, 'Testing that tags have been deleted once a DAO has been deleted from the search index'); }