CakePHP:控制器使用另一个模型而不是自己的模型

发布于 2024-12-20 06:46:07 字数 1018 浏览 0 评论 0原文

情况如下所示:我有两个带有控制器和所有内容的模型,WrittenTest 和 WrittenTestAnswer。问题是,每当我尝试从 WrittenTestsController(之前使用 $this->loadModel())和它自己的 WrittenTestAnswersController 访问模型 WrittenTestAnswer 时,它都会以某种方式访问​​ WrittenTest。我注意到当数据没有保存到WrittenTestAnswer时,$this->WrittenTestAnswer->find()也从writing_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'); 

返回 write_tests 的第一行。知道发生了什么事吗?

The situations looks like this: I have two models with controllers and everything, WrittenTest and WrittenTestAnswer. The problem is that whenever I try to access model WrittenTestAnswer, both from WrittenTestsController (using $this->loadModel() before) and from its own WrittenTestAnswersController, it somehow accesses WrittenTest instead. I noticed it when data wasn't saved to WrittenTestAnswer, $this->WrittenTestAnswer->find() also returned data from written_tests table. I have no idea what's going on. I checked names and stuff so many times already. I am using CakePHP 1.3. Thanks for any help.

EDIT:
code from WrittenTestAnswer model:

class WrittenTestAnswer extends AppModel {

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

}

EDIT so for example this (in WrittenTestAnswersController)

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

outputs WrittenTest. And

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

returns first row from written_tests. Any idea what's going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

计㈡愣 2024-12-27 06:46:07

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

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

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

How are you accessing the model? If the relationships are setup correctly, you shouldn't need to use $this->loadModel() at all. You can just access the related model through its parent like so:

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

This might not be the cause of your problems but we cannot tell until you actually post the code that's not working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文