user1870982
user1870982

Reputation: 9

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 :

    <?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

Upvotes: 0

Views: 3875

Answers (1)

arilia
arilia

Reputation: 9398

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

So when you call $applicantDesiredEducation->education_field_of_study_sub->name_en you get that error

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);   
}

Upvotes: 1

Related Questions