[英]cakePHP - How to solve a 'Trying to get property of non-object' error
hello i received this error message when i run my application on the server : 你好,我在服务器上运行应用程序时收到此错误消息:
Notice (8): Trying to get property of non-object [APP/Template/ApplicantEducationNeeds/view.ctp, line 52]
and this is the lines of code in view.ctp : 这是view.ctp中的代码行:
<?php
use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\Datasource\ConnectionManager;
use Cake\Error\Debugger;
use Cake\Network\Exception\NotFoundException;
$this->layout = 'userProfile';
if (!Configure::read('debug')):
throw new NotFoundException();
endif;
//echo debug($applicantDesiredEducations);
?>
<div class='col-md-12'>
<div class="applicantGenerals view large-9 medium-8 columns content">
<div class ='col-md-4'>
<h3><?= __('Desired Education') ?></h3>
<table class="table">
<thead>
<tr>
<th><?= __('Field Of Studies') ?></th>
<th class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($applicantDesiredEducations as $applicantDesiredEducation): ?>
<tr>
//here the lines of 50's
<td><?= $_language == 'en_US' ? h($applicantDesiredEducation->education_field_of_study_sub->name_en) : h($applicantDesiredEducation->education_field_of_study_sub->name_ara) ?></td>
but notice when i ruining this code in my local machine its works fine , any promising solution it will be precipitation 但是请注意,当我在本地计算机上破坏此代码时,它的工作正常,任何有前途的解决方案都会是沉淀
just a guess, since you are not providing much information about your error: 只是一个猜测,因为您没有提供有关错误的太多信息:
you are looping through $applicantDesiredEducations
, but probably not every $applicantDesiredEducation
has an education_field_of_study_sub
您正在遍历$applicantDesiredEducations
,但是可能不是每个$applicantDesiredEducation
都有一个education_field_of_study_sub
So when you call $applicantDesiredEducation->education_field_of_study_sub->name_en
you get that error 因此,当您调用$applicantDesiredEducation->education_field_of_study_sub->name_en
会收到该错误
you have to insert a check on the exixtence of the property, something like 您必须插入属性的检查,例如
if(isset($applicantDesiredEducation->education_field_of_study_sub))
{
echo h($applicantDesiredEducation->education_field_of_study_sub->name_en);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.