我有以下型号:
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')
);
模型预算和项目如下
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')
);
我已经成功创建了一个带脚手架的控制器,可以添加/删除/更新调整但是当我烘焙代码时,我收到消息“错误:在非对象上调用成员函数find()”当我尝试添加时调整。
错误发生在以下行:
$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'));
关闭cource没有模型SBudget,TBudget,SProject,TProject所以我编辑了如下代码:
$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'));
我添加了这条线 public $ uses = array('Budget','Project');
调整控制器类中的。不幸的是问题仍然存在,我总是得到“错误:在非对象”消息上调用成员函数find()。
答案 0 :(得分:0)
您在第一个代码块中的belongsTo关联中使用的别名是错误的。而不是Source_Budget
它应该是SourceBudget
。同样,对于其他别名,请删除下划线。
答案 1 :(得分:0)
我找到了!!!
问题是,控制器中生成的变量和传递给具有set的视图的变量未被视图正确使用。让我解释一下:
烘焙产生了一个变量
$ sourceBudgets = $ this-> Adjustment-> Budget-> find('list'); 并将其传递给添加视图。 但是,添加视图使用名为weldudget_id的变量。与模型中声明的外键字段相同。
答案 2 :(得分:0)
尽量保持模型中的名称一致,例如。而不是:
'的 SourceBudget 强>' =>阵列('的className' =>'预算'' FOREIGNKEY&# 39; =>'的 s_budget_id 强>&#39),
写下:
'的 SBudget 强>' =>阵列('的className' =>'预算'' FOREIGNKEY&# 39; =>'的 s_budget_id 强>&#39),
或(您需要更新数据库):
'的 SourceBudget 强>' =>阵列('的className' =>'预算'' FOREIGNKEY&# 39; =>'的 source_budget_id 强>&#39),
懒惰人可以简单地说:
'的 SBudget 强>' =>阵列('的className' =>'预算&#39),
或:
'的 SourceBudget 强>' =>阵列('的className' =>'预算&#39),
嗯,我花了几个小时才意识到这一点。 CakePHP家伙"是会议和配置的忠实粉丝。 ; - )