简体   繁体   English

cakePHP-如何解决“尝试获取非对象的属性”错误

[英]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.

相关问题 如何解决这个试图获取Laravel中非对象错误的属性 - How To Solve This Trying to get property of non-object Error In Laravel 如何解决laravel中的错误``试图获取非对象的属性&#39;&#39; - How to solve error 'Trying to get property of non-object' in laravel 如何在 Laravel 6 中解决这个问题? 试图获取非对象的属性“名称” - How to solve this in Laravel 6? Trying to get property 'name' of non-object 如何解决试图在 Laravel 中获取非对象的属性 - How to solve Trying to get property of non-object in Laravel 如何解决尝试获取非对象的属性? - How can I solve Trying to get property of non-object? 如何解决试图在Laravel中获取非对象的属性? - How to solve Trying to get property of non-object in Laravel? 如何解决PHP代码中的“试图获取非对象错误的属性”和“未定义变量:ID”? - how to solve 'Trying to get property of non-object error' & 'Undefined variable: id' in php code? 如何解决此 CodeIgniter 错误消息? “注意:尝试在 CodeIgniter 中获取非对象的属性” - How to solve this CodeIgniter error message? "Notice: Trying to get property of non-object in CodeIgniter" 如何解决消息:试图在 Codeigniter 中获取非对象错误的属性? - How to solve Message: Trying to get property of non-object error in Codeigniter? 如何解决此错误尝试在createProduct刀片文件中获取非对象的属性“ id”? - How to solve this error Trying to get property 'id' of non-object in createProduct blade file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM