0

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?

2
  • 1
    Obviously $modelProp is not an object, so you have to do some debugging and figure out what went wrong in obtaining the value for that variable. Commented Dec 28, 2016 at 1:21
  • I think I've found the answer. Commented Dec 29, 2016 at 19:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.