我正在尝试加载一些我稍后会发送的数据,但我收到了这个通知:“试图获取非对象的属性”。
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
然后我收到了这个错误:
无法发出回应;已发送标头
这是我使用find()的代码:
$this->loadModel('DespesaOrcamentarias');
$despesas = $this->DespesaOrcamentarias->find('all', array(
'contain' => array('PlanoDespesas')
))->toArray();
这是模型实体中导致问题的功能代码:
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;
}
我无法弄清楚这里发生了什么。我做了一些研究,但我没有发现任何有用的事情。
我只是一个初学者,但在我看来,这与函数生成的虚拟字段有关,但我没有办法避免这种情况。
相关错误: 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
编辑:我已经解决了这个问题。我在构造函数中添加了以下验证,然后它起作用了:if(isset($this->selecionados)){
$this->selecionados = $this->selecionados;
}
我认为这是因为它只会设置var selecionados,如果它不是空的。我是对的吗?