CakePHP bindModel 工作,预设模型关联不工作
我正在开发一个个人项目。
我有两个模型“Show”和“Episode”。我有一个控制器“Ops”。
显示模型:
class Show extends AppModel
{
var $name = 'Show';
var $hasMany = array(
'Episode' => array(
'className' => 'Episode',
'foreignKey' => 'show_id'
)
);
}
剧集模型:
class Episode extends AppModel
{
var $name = 'Episode';
var $belongsTo = array(
'Show' => array(
'className' => 'Show',
'foreignKey' => 'show_id'
)
);
}
Ops 控制器:
class OpsController extends AppController
{
var $name = 'Ops';
var $uses = array('Show','Episode');
function index()
{
$episodes = $this->Episode->find('all',array(
'limit' => 10,
'order' => array('Episode.first_aired' => 'DESC'),
)
);
debug($this->Episode);
debug($episodes);
}
}
运行 Ops 控制器时,我得到了我想要的“Episode”记录,但没有根据“belongsTo”配置中的“show_id”获得关联的“Show”记录。看起来它根本没有引用模型,因为我可以故意破坏模型类,而请求仍然继续。
经过大量检查、研究和测试后,我能够通过在 find() 请求之前将以下内容添加到 Ops 控制器中来使其工作:
$this->Episode = ClassRegistry::init('Episode');
$this->Episode->bindModel(
array('belongsTo' => array(
'Show' => array(
'className' => 'Show'
)
)
)
);
现在,虽然这有效,但我仍然想知道为什么我的模型不工作被正确调用。任何帮助将不胜感激。谢谢!
I am in development of a personal project.
I have two models 'Show' and 'Episode'. I have one controller 'Ops'.
Show model:
class Show extends AppModel
{
var $name = 'Show';
var $hasMany = array(
'Episode' => array(
'className' => 'Episode',
'foreignKey' => 'show_id'
)
);
}
Episode model:
class Episode extends AppModel
{
var $name = 'Episode';
var $belongsTo = array(
'Show' => array(
'className' => 'Show',
'foreignKey' => 'show_id'
)
);
}
Ops controller:
class OpsController extends AppController
{
var $name = 'Ops';
var $uses = array('Show','Episode');
function index()
{
$episodes = $this->Episode->find('all',array(
'limit' => 10,
'order' => array('Episode.first_aired' => 'DESC'),
)
);
debug($this->Episode);
debug($episodes);
}
}
When running the Ops controller I get the 'Episode' records like I want but don't get the associated 'Show' record based on the 'show_id' in the 'belongsTo' configuration. It appears that it is not referencing the model at all as I can purposefully break the model class an the request still goes on.
After doing a lot of checking, researching, and testing, I was able to get it to work by adding the following into the Ops controller before the find() request:
$this->Episode = ClassRegistry::init('Episode');
$this->Episode->bindModel(
array('belongsTo' => array(
'Show' => array(
'className' => 'Show'
)
)
)
);
Now while this works I would still like to know why my models are not being called properly. Any help would be most appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您以同样的方式查询 Show 会发生什么?
您确定
id
字段定义正确吗?在两个表上,您都应该有
id
(INTsize),在episodes 上,还应该有show_id
(INTsize)。如果它是根据 Cake 约定设置的,您应该能够删除
'foreignKey' =>; 'show_id'
行,Cake 会自行解决。What happens if you query Show in the same way?
Are you certain the
id
fields are defined correctly?On both tables you should have
id
(INTsize) and on episodes there should also beshow_id
(INTsize).If it's set up according to Cake convention, you should be able to remove the
'foreignKey' => 'show_id'
line and Cake will sort it out itself.`听起来 Cake 没有使用您的模型文件,而是根据表格自动生成一些模型文件。
这听起来很愚蠢,但请检查文件夹和文件名是否有拼写错误以及它们是否为小写。
It sounds like Cake isn't using your model files and instead automagically generating some based on the tables.
It sounds dumb, but check the folders and file names for spelling errors and that they are lowercase.
在您的 Show 模型中,您将 Episode 外键设置为 show_id,它应该是 Episode_id。但是,我不认为这是造成问题的原因。
据我所知,您没有更改任何 CakePHP 命名约定,因此只需删除定义关联的数组并保留为字符串,例如,
这可能不起作用,但我之前遇到过类似的问题,这解决了它。祝你好运。
In your Show model, you have the Episode foreign key set to show_id, which should be episode_id. However, I don't think that is causing the problem.
You aren't changing any CakePHP naming conventions, from what I can tell, so just remove the arrays that define the association and leave as string, e.g.
This may not work, but I have bumped into similar issues before and this resolved it. Good luck.