Strange Behavior

I have a simple function that returns the name of a category. I tested it as shown below with an exit statement after the echo test.


	

public function getCategory($id = null) 	

{ 		

       $category=FaqCategories::model()->findByPk($id); 		

       echo $category->category; 		

       exit; 		

       return $category; 	

}

It echos back the correct answer. I then took out the exit, and Yii errors out saying that I am "Trying to get property of non-object" and pointing to the echo line which previously just worked perfectly. No matter where I try to get the $category->category result, unless I exit Yii immediately it errors. What am I doing wrong?

I am guessing you are calling getCategory() more than once in the request, and it works the first time, but one of the latter times, you are probably passing an invalid ID to it

just to add on jonah post…

findByPk returns null if nothing is found… implement some checking like


 if($category == null) echo "Wrong ID: ".$id;

This does not return the name of the category but the category AR

to return the name of the category :




return $category->category; 	



You were right. A bad id tripped it up. :rolleyes:

Many thanks!