public static function getNamaItemById($id)
{
$sql = "select itmname from item where itmid='$id'";
$itemname = item::model()->findBySql($sql);
return $itemname->itmname;
}
I have the codes above and when I request from view like getNamaItemById(9) no records. so I got this error "Trying to get property of non-object". please help so I still can use that query with result empty/null not error instead.
findBySql returns NULL if nothing is found… so now you have to decide what you want to return from your method in case nothing is found… do you return false… or you return empty string… and add something like this:
...
$itemname = item::model()->findBySql($sql);
if($itemname===null)
return ''; // <-- return empty if nothing is found
else
return $itemname->itmname;