【问题标题】:CakePHP: controller uses another model instead of its ownCakePHP:控制器使用另一个模型而不是自己的模型
【发布时间】:2011-12-09 09:08:10
【问题描述】:

情况如下所示:我有两个模型,带有控制器和所有东西,WrittenTest 和 WrittenTestAnswer。问题是,每当我尝试从 WrittenTestsController(之前使用 $this->loadModel())和它自己的 WrittenTestAnswersController 访问模型 WrittenTestAnswer 时,它都会以某种方式访问​​ WrittenTest。当数据未保存到 WrittenTestAnswer 时,我注意到了这一点,$this->WrittenTestAnswer->find() 也从written_tests 表中返回了数据。我不知道是怎么回事。我已经检查了很多次名字和东西了。我正在使用 CakePHP 1.3。谢谢你的帮助。

编辑: 来自 WrittenTestAnswer 模型的代码:

class WrittenTestAnswer extends AppModel {

public $name = 'WrittenTestAnswer';
public $displayField = 'written_test_answer';
public $belongsTo = array(
      'WrittenTest' => array(
            'className' => 'WrittenTest', 
            'foreignKey' => 'written_test_id', 
      ), 
);

}

编辑例如这个(在 WrittenTestAnswersController 中)

debug($this->WrittenTestAnswer->name); 

输出WrittenTest。和

$this->WrittenTestAnswer->find('first'); 

从written_tests 返回第一行。知道发生了什么吗?

【问题讨论】:

  • 你能粘贴WrittenTestAnswer模型类的代码吗?

标签: php cakephp model controller


【解决方案1】:

您如何访问模型?如果关系设置正确,则根本不需要使用$this->loadModel()。您可以像这样通过其父级访问相关模型:

$this->WrittenTest->WrittenTestAnswer->find('all');

这可能不是您的问题的原因,但在您实际发布不起作用的代码之前,我们无法判断。

【讨论】:

  • 我已经说过,即使我从 WrittenTestAnswersController 访问 WrittenTestAnswer 也不起作用,所以这没关系。这是一个不起作用的简单查找 - 它从 WrittenTest 返回数据。
  • 你没有说明你是怎么做的,贴出不起作用的代码!
猜你喜欢
  • 1970-01-01
  • 2012-02-27
  • 1970-01-01
  • 2011-08-20
  • 2012-01-03
  • 2012-03-30
  • 2017-05-09
  • 1970-01-01
  • 2016-08-30
相关资源
最近更新 更多