0

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.

leonidou
  • 3
  • 1
  • 4
  • What -exact- line are you getting the error on, because you're showing 5 lines. Furthermore, You're defining your 'belongs to Using "Source_Budget", but referring to it as 'SourceBudget'. And finally; this would at least be referring to *existing* variables; `$this->set(compact('sourceBudgets', 'targetBudgets', 'sourceProjects', 'targetProjects')); – thaJeztah Feb 17 '13 at 16:37
  • thanks for your comment. I get the error on "$sourceBudgets = $this->Adjustment->SourceBudget->find('list');" SourceBudget was produced by bake same as $this->set(compact('sourceBudgets', 'targetBudgets', 'sourceProjects', 'targetProjects')) – leonidou Feb 17 '13 at 17:34

3 Answers3

0

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.

ADmad
  • 8,102
  • 16
  • 18
  • Thank you for your answer. I removed the underscore from the associations and I no more have the error. But the $sourceBudgets = $this->Adjustment->SourceBudget->find('list'); are not producing any data. so the lists in my add view are empty. changing my code to $sourceBudgets = $this->Budget->find('list'); still produces empty lists on add view – leonidou Feb 17 '13 at 20:04
  • Well does the tables contain records? Check the query log and ensure the queries generated are as expected. Run the same queries directly in mysql and check. – ADmad Feb 18 '13 at 06:30
0

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.

leonidou
  • 3
  • 1
  • 4
0

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

staho
  • 1