Skip to content

Commit

Permalink
Merge pull request #330 from alphadevx/feature/75-relation-getrelated
Browse files Browse the repository at this point in the history
#75 - merged the Relation::getRelatedObject() and Relation::getRelate…
  • Loading branch information
alphadevx committed Mar 18, 2018
2 parents 19d170d + 075584b commit ff226b4
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 83 deletions.
2 changes: 1 addition & 1 deletion Alpha/Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ private function renderTags()
$html = '';

if ($relation instanceof Relation) {
$tags = $relation->getRelatedObjects();
$tags = $relation->getRelated();

if (count($tags) > 0) {
$html .= '<p>Tags:';
Expand Down
2 changes: 1 addition & 1 deletion Alpha/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';

Expand Down
2 changes: 1 addition & 1 deletion Alpha/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 .= '<p>Tags: ';
Expand Down
4 changes: 2 additions & 2 deletions Alpha/Controller/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function doGET($request)
try {
$record->load($ActiveRecordID);

$tags = $record->getPropObject('tags')->getRelatedObjects();
$tags = $record->getPropObject('tags')->getRelated();

ActiveRecord::disconnect();

Expand Down Expand Up @@ -340,7 +340,7 @@ public function doPOST($request)
try {
$record->load($ActiveRecordID);

$tags = $record->getPropObject('tags')->getRelatedObjects();
$tags = $record->getPropObject('tags')->getRelated();

ActiveRecord::begin();

Expand Down
4 changes: 2 additions & 2 deletions Alpha/Model/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions Alpha/Model/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ public function getArticleScore()
*/
public function getArticleVotes()
{
$votes = $this->votes->getRelatedObjects();
$votes = $this->votes->getRelated();

return $votes;
}
Expand Down Expand Up @@ -505,7 +505,7 @@ public function checkUserVoted()
*/
public function getArticleComments()
{
$comments = $this->comments->getRelatedObjects();
$comments = $this->comments->getRelated();

return $comments;
}
Expand Down
77 changes: 34 additions & 43 deletions Alpha/Model/Type/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
4 changes: 2 additions & 2 deletions Alpha/Util/Search/SearchProviderTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion Alpha/View/ArticleView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Alpha/View/Widget/RecordSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/Alpha/Test/Controller/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions test/Alpha/Test/Controller/TagControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 = '';
Expand Down Expand Up @@ -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) {
Expand All @@ -197,7 +197,7 @@ public function testDoDELETE()

$response = $front->process($request);

$tags = $article->getPropObject('tags')->getRelatedObjects();
$tags = $article->getPropObject('tags')->getRelated();

$found = false;
$tagID = '';
Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions test/Alpha/Test/Model/TagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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());
Expand Down
24 changes: 8 additions & 16 deletions test/Alpha/Test/Model/Type/RelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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();
Expand All @@ -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');
Expand All @@ -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');
}
}
6 changes: 3 additions & 3 deletions test/Alpha/Test/Util/Search/SearchProviderTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
}
Expand Down

0 comments on commit ff226b4

Please sign in to comment.