0

我有以下模型:

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

场外没有模型 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()”消息。

4

3 回答 3

0

您在第一个代码块中的 belongsTo 关联中使用的别名是错误的。而不是Source_Budget应该是SourceBudget. 同样,对于其他别名,请去掉下划线。

于 2013-02-17T19:39:51.010 回答
0

我找到了 !!!

问题是在控制器中生成并通过 set 传递给视图的变量没有被视图正确使用。让我解释:

bake 产生了一个变量

$sourceBudgets = $this->Adjustment->Budget->find('list'); 并将其传递给添加视图。然而,添加视图使用了一个名为 s_budget_id 的变量。与模型中声明的外键字段相同。

于 2013-02-21T20:43:34.540 回答
0

尽量保持模型中的名称一致,例如。代替:

' SourceBudget '=>array('className'=>'Budget','foreignKey'=>' s_budget_id '),

写:

' SBudget '=>array('className'=>'Budget','foreignKey'=>' s_budget_id '),

或(您将需要更新您的数据库):

' SourceBudget '=>array('className'=>'Budget','foreignKey'=>' source_budget_id '),

因此,懒惰的人可以简单地说:

' SBudget '=>array('className'=>'Budget'),

或者:

' SourceBudget '=>array('className'=>'Budget'),

好吧,我花了几个小时才意识到这一点。CakePHP 的家伙“是约定优于配置的忠实拥护者”;-)

于 2013-06-27T16:40:50.923 回答