CakePHP:控制器使用另一个模型而不是自己的模型
情况如下所示:我有两个带有控制器和所有内容的模型,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您如何访问该模型?如果关系设置正确,您根本不需要使用
$this->loadModel()
。您可以通过其父级访问相关模型,如下所示:这可能不是问题的原因,但在您实际发布不起作用的代码之前我们无法判断。
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 might not be the cause of your problems but we cannot tell until you actually post the code that's not working.