The aliases you have used in the belongsTo association in first block of code are wrong. Instead of Source_Budget
it should be SourceBudget
. Similarly for other aliases drop the underscore.
Cakephp Error: Call to a member function find() on a non-object
-
10-03-2022 - |
Frage
I have the following model:
class Adjustment extends AppModel {
var $name = 'Adjustment';
var $belongsTo = array(
'Source_Budget'=>array('className'=>'Budget','foreignKey'=>'s_budget_id'),
'Target_Budget'=>array('className'=>'Budget','foreignKey'=>'t_budget_id'),
'Source_Project'=>array('className'=>'Project','foreignKey'=>'s_project_id'),
'Target_Project'=>array('className'=>'Project','foreignKey'=>'t_project_id')
);
model budget and project are as below
class Budget extends AppModel {
var $name='Budget';
var $belongsTo = array('Year');
var $hasMany=array('Project','Payment','Revenue',
'AdjustmentFrom'=>array('className'=>'Adjustment','foreignKey'=>'s_budget_id'),
'AdjustmentTo'=>array('className'=>'Adjustment','foreignKey'=>'t_budget_id'),
'TransferFrom'=>array('className'=>'Transfer','foreignKey'=>'s_budget_id'),
'TransferTo'=>array('className'=>'Transfer','foreignKey'=>'t_budget_id')
);
class Project extends AppModel {
var $name = 'Project';
var $belongsTo = array('Budget','Year');
var $hasMany=array('Payment','Revenue',
'AdjustmentFrom'=>array('className'=>'Adjustment','foreignKey'=>'s_project_id'),
'AdjustmentTo'=>array('className'=>'Adjustment','foreignKey'=>'t_project_id'),
'TransferFrom'=>array('className'=>'Transfer','foreignKey'=>'s_project_id'),
'TransferTo'=>array('className'=>'Transfer','foreignKey'=>'t_project_id')
);
I have succesfully created a controller with scaffolding which adds/deletes/updates adjustments but when I bake the code I get the message "Error: Call to a member function find() on a non-object " when I try to add an adjustment.
The error is on the following line:
$sourceBudgets = $this->Adjustment->SourceBudget->find('list');
$targetBudgets = $this->Adjustment->TargetBudget->find('list');
$sourceProjects = $this->Adjustment->SourceProject->find('list');
$targetProjects = $this->Adjustment->TargetProject->find('list');
$this->set(compact('sBudgets', 'tBudgets', 'sProjects', 'tProjects'));
Off cource there is no model SBudget, TBudget, SProject, TProject so I edited the code as below:
$sourceBudgets = $this->Adjustment->Budget->find('list');
$targetBudgets = $this->Adjustment->Budget->find('list');
$sourceProjects = $this->Adjustment->Project->find('list');
$targetProjects = $this->Adjustment->Project->find('list');
$this->set(compact('sBudgets', 'tBudgets', 'sProjects', 'tProjects'));
and I added the line public $uses = array('Budget', 'Project');
in the Adjustment controller class. Unfortunatly the problem persists and I always get the "Error: Call to a member function find() on a non-object" message.
Lösung
Andere Tipps
I found it !!!
The problem is that the variables generated in the controller and passed to views with set are not used by view correctly. Let me explain:
bake produced a variable
$sourceBudgets = $this->Adjustment->Budget->find('list'); and passed it to the add view. However the add view was using a variable called s_budget_id. same as the foreign key field as declared in the model.
Try to keep the names in your model consistent, eg. instead of:
'SourceBudget'=>array('className'=>'Budget','foreignKey'=>'s_budget_id'),
write either:
'SBudget'=>array('className'=>'Budget','foreignKey'=>'s_budget_id'),
or (you will need to update your database):
'SourceBudget'=>array('className'=>'Budget','foreignKey'=>'source_budget_id'),
Lazy one can simply put, accordingly:
'SBudget'=>array('className'=>'Budget'),
or:
'SourceBudget'=>array('className'=>'Budget'),
Well, it took me hours to realize that. CakePHP guys "are big fans of convention over configuration" ;-)