Getting "Trying to get property of non-object" when loading data through find()

626 views Asked by At

I'm trying to load some data that I'll send to view later but I'm getting this notice: "Trying to get property of non-object".

App\Model\Entity\DespesaOrcamentaria::_getSelecionados() - APP/Model\Entity\DespesaOrcamentaria.php, line 54
Cake\ORM\Entity::get() - CORE\src\Datasource\EntityTrait.php, line 294
Cake\ORM\Entity::__get() - CORE\src\Datasource\EntityTrait.php, line 135
App\Model\Entity\DespesaOrcamentaria::__construct() - APP/Model\Entity\DespesaOrcamentaria.php, line 63
Cake\ORM\ResultSet::_groupResult() - CORE\src\ORM\ResultSet.php, line 567
Cake\ORM\ResultSet::_fetchResult() - CORE\src\ORM\ResultSet.php, line 465
Cake\ORM\ResultSet::valid() - CORE\src\ORM\ResultSet.php, line 268
iterator_to_array - [internal], line ??
Cake\ORM\ResultSet::toArray() - CORE\src\Collection\CollectionTrait.php, line 496
Cake\ORM\Query::toArray() - CORE\src\Datasource\QueryTrait.php, line 293
App\Controller\Orcamento\RelatoriosController::demonstrativoCategoriasEconomicas() - APP/Controller\Orcamento\RelatoriosController.php, line 38
Cake\Controller\Controller::invokeAction() - CORE\src\Controller\Controller.php, line 435
Cake\Http\ActionDispatcher::_invoke() - CORE\src\Http\ActionDispatcher.php, line 122
Cake\Http\ActionDispatcher::dispatch() - CORE\src\Http\ActionDispatcher.php, line 96
Cake\Http\BaseApplication::__invoke() - CORE\src\Http\BaseApplication.php, line 83
Cake\Http\Runner::__invoke() - CORE\src\Http\Runner.php, line 65

And then I get this error:

Unable to emit response; headers already sent

This is the code where I'm using the find():

$this->loadModel('DespesaOrcamentarias');
    $despesas = $this->DespesaOrcamentarias->find('all', array(
        'contain' => array('PlanoDespesas')
    ))->toArray();

And this is the function code from the Model Entity that's causing the issue:

public function _getSelecionados(){
$selecionados = [];
if(!$this->isNew()){
  foreach ($this->models as $idx => $model) {
    $modelProp = $this->{Inflector::underscore(Inflector::singularize($model['nome']))};
    $selecionados[$idx.'.'.$modelProp->id] = $modelProp->codigoNome;
  }
}
return $selecionados;
}

I can't figure out what's happening here. I did some research but I didn't find anything helpful for my case.

I'm just a beginner but it seems to me that this is related to the virtual fields the function generates, but I didn't find a way to avoid this.

Related errors: Fetch only some fields on find in CAKEPHP CakePHP "Trying to get property of non-object" error CakePHP 3 : Trying to get property of non-object cakePHP - How to solve a 'Trying to get property of non-object' error

EDIT: I've solved the problem. I added the following validation in the Constructor and then it worked:

if(isset($this->selecionados)){
   $this->selecionados = $this->selecionados;
}

I think this happens because it will only set the var selecionados if it's not empty. Am I right?

0

There are 0 answers