我在Yii中遇到Trying to get property of non-object
错误。代码曾经工作过,然而一个同事做了一些改变,现在出现了一个错误,我不能为我的生活弄清楚什么是坏的。
以下是来自applciation.log的堆栈跟踪和错误消息:
2014/07/02 09:39:35 [error] [php] Trying to get property of non-object (/home/jhvisser/web/inventory/web/inventory/protected/models/Inventory.php:454)
Stack trace:
#0 /home/jhvisser/web/inventory/web/framework/web/actions/CAction.php(104): ReflectionMethod->invokeArgs()
#1 /home/jhvisser/web/inventory/web/framework/web/actions/CInlineAction.php(48): CInlineAction->runWithParamsInternal()
#2 /home/jhvisser/web/inventory/web/framework/web/CController.php(300): CInlineAction->runWithParams()
#3 /home/jhvisser/web/inventory/web/framework/web/filters/CFilterChain.php(134): InventoryController->runAction()
#4 /home/jhvisser/web/inventory/web/framework/web/filters/CFilter.php(41): CFilterChain->run()
#5 /home/jhvisser/web/inventory/web/framework/web/CController.php(1144): CAccessControlFilter->filter()
#6 /home/jhvisser/web/inventory/web/framework/web/filters/CInlineFilter.php(59): InventoryController->filterAccessControl()
#7 /home/jhvisser/web/inventory/web/framework/web/filters/CFilterChain.php(131): CInlineFilter->filter()
#8 /home/jhvisser/web/inventory/web/framework/web/CController.php(283): CFilterChain->run()
#9 /home/jhvisser/web/inventory/web/framework/web/CController.php(257): InventoryController->runActionWithFilters()
#10 /home/jhvisser/web/inventory/web/framework/web/CWebApplication.php(277): InventoryController->run()
#11 /home/jhvisser/web/inventory/web/framework/web/CWebApplication.php(136): CWebApplication->runController()
#12 /home/jhvisser/web/inventory/web/framework/base/CApplication.php(158): CWebApplication->processRequest()
#13 /home/jhvisser/web/inventory/web/inventory/index.php(14): CWebApplication->run()
REQUEST_URI=/inventory/web/inventory/index.php?r=inventory/update&id=14760
如果你查看下面的代码,这是在这个函数中发生的错误,特别是在包含以下代码的行:$attr = $val->attributes;
。完整的功能代码如下。我很困惑这里的错误是什么。
//get various dropdown fields for UI and validation
public function getDropdown($validate = false)
{
//make objects
$feed['equipmentType'] = AuxEquipmentType::model()->findAll();
$feed['sponsors'] = AuxSponsors::model()->findAll();
$feed['licences'] = AuxLicences::model()->findAll();
$feed['groups'] = AuxGroups::model()->findAll();
$feed['policyEight'] = AuxPolicyEight::model()->findAll();
$feed['supportClass'] = AuxSupportClass::model()->findAll();
$feed['special'] = AuxSpecial::model()->findAll();
$feed['domain'] = AuxDomain::model()->findAll();
if(!$validate && MAINTAIN_ON)
{
$maintain = new Maintain();
$feed['freeIP'] = $maintain->getFreeIP();
}
//get keys of which fields to take and make the string display and value columns with
$headers = array(
'sponsors' => array('key' => 'code', 'val' => array('code', 'fullName', 'description')),
'licences' => array('key' => 'lKey', 'val' => array('licenceName')),
'groups' => array('key' => 'gr_name', 'val' => array('gr_name')),
'supportClass' => array('key' => 'value', 'val' => array('value')),
'special' => array('key' => 'value', 'val' => array('value')),
'equipmentType' => array('key' => 'value', 'val' => array('value')),
'domain' => array('key' => 'domain', 'val' => array('domain')),
'freeIP' => array('key' => 'name', 'val' => array('rang')),
'policyEight' => array('key' => 'value', 'val' => array('value')),
);
$out = array();
foreach($feed as $field => $value)
{
foreach($value as $fld => $val)
{
$attr = $val->attributes; //** THIS LINE IS GIVING THE ERROR **
if(!$validate)
{
if($field == 'freeIP')
$attr = $val;
$option = array();
foreach($headers[$field]['val'] as $va)
$option[] = $attr[$va];
$out[$field][$attr[$headers[$field]['key']]] = implode(' - ', $option);
}
else
$out[$field][] = $attr[$headers[$field]['key']];
}
}
return $out;
}
答案 0 :(得分:1)
代码块:
if($field == 'freeIP')
$attr = $val;
位于错误的位置,因为该数组与其他$ feed元素的结构不同。
将代码更改为以下内容以更正错误:
if($field == 'freeIP')
$attr = $val;
else
$attr = $val->attributes;