Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

Getting Error and Returned value with DataObject::get


Reply


989 Views

Avatar
jdi

Community Member, 34 Posts

26 December 2009 at 8:38am

Hey crew,

Merry Christmas guys! I hope you've all had a great christmas.

I have an issue and need some help to resolve something which is in the way of my progression with developing a module:

public function getRelatedArticles($currentDoc=false) {

         //$currentDoc is a string taken from the URL to define the Article name
      
         $currentTitle = str_replace('-',' ',$currentDoc);
      
         $currentArticle = DataObject::get_one('ArticleManagerArticle' , '`Title` = \''.$currentTitle.'\'');

      //'Tags' is part of the DataObject set for each Article

      $QueryTags = explode(" ", $currentArticle->Tags);
      $i = 0;
      $numValues = count($QueryTags);
      $RelatedArticles = new DataObjectSet();
      
      while($i < $numValues) {
         $QueryTag = '%'.$QueryTags[$i].'%';
         $RelatedArticle = DataObject::get('ArticleManagerArticle','`Title` LIKE \''.$QueryTag.'\'');
         $RelatedArticles->push($RelatedArticle);
         
         $i++;
      }
      
      return $RelatedArticles;
   
   }

So the 2 things that are occurring here is:

1. '$currentArticle->Tags' returns the DataObject set value (e.g "Article Important News Story") AND returns this error "[Notice] Trying to get property of non-object", this is because the value $currentTitle doesn't match the DataObject entry 'Title' exactly (it's lower case). But why does it matter!? I'm still getting the value I want to continue to function...?

2. Can someone assist me with populating my new DataObject inside the while loop? Basically I want to loop to call every article it can related to each word in the 'Tags' string and then let me display them in a template inside

<% control RelatedArticles %>$Title<% end_control %>

currently my DataObject $RelatedArticles set isn't being created correctly :(?

Sorry about the big post! Feel free to comment on anything inside the question as any and all solutions or comments are very welcome!

Jackson.