我说错了,
Fatal error: Call to a member function getId() on null.
但是如何检查元素,这是我的代码
$infoArray['groupId'] = $info->getId();
$infoArray['name'] = $info->getName();
$infoArray['addressLine1'] = $info->getAddressLine1();
$infoArray['addressLine2'] = $info->getAddressLine2();
$infoArray['isActive'] = $info->getActive();
$infoArray['countryId'] = $info->getCountry()->getId(); //Here is my error line
$infoArray['countryName'] = $info->getCountry()->getName();
$infoArray['stateId'] = $info->getState()->getId();
$infoArray['stateName'] = $info->getState()->getName();
$infoArray['cityId'] = $info->getCity()->getId();
$infoArray['cityName'] = $info->getCity()->getName();
$infoArray['areaId'] = $info->getArea()->getId();
$infoArray['areaName'] = $info->getArea()->getName();
$infoArray['zipcode'] = $info->getZipcode();
答案 0 :(得分:4)
将行替换为:
$infoArray['countryId'] = $info->getCountry() ? $info->getCountry()->getId() : null;
对与关联相对应的每一行(可以为null)执行相同操作,如果对象为null,则返回null
。
答案 1 :(得分:2)
使用is_null($ object_name)检查