cakephp 测试套件:导入 HABTM 的固定装置表

发布于 2024-10-28 23:05:41 字数 1357 浏览 4 评论 0原文

我有一个模型 Post -> hasAndBelongsToMany -> 并且

为了测试,我为每个模型创建了固定装置,例如,模型帖子的固定装置看起来像这样

class PostFixture extends CakeTestFixture {
    var $import = array('model' => 'Post', 'records' => true, 'connection' => 'fixtures');
}

一切都非常适合该模型,但是当我尝试为 HABTM 关系创建固定装置时,使用同样的方法不起作用:

class PostsTagFixture extends CakeTestFixture {
    var $import = array('model' => 'PostTag', 'records' => true, 'connection' => 'fixtures');
}

CakePHP 生成的 SQL 如下,这

CREATE TABLE `service_types_technicals` (
    `technical_id` int(20) NOT NULL AUTO_INCREMENT,
    `service_type_id` int(20) NOT NULL AUTO_INCREMENT,
    `id` varchar(255) NOT NULL, PRIMARY KEY  (`id`))    ;

是不正确的,因为该表没有名为 id 的字段。

然后,我尝试了这个:

class PostsTagFixture extends CakeTestFixture {
    var $name = 'PostsTag';
    var $import = array('table' => 'posts_tags', 'records' => true, 'connection' => 'fixtures');
}

再一次,错误,但这一次 SQL 是:

CREATE TABLE `service_types_technicals` (
    `technical_id` int(20) NOT NULL AUTO_INCREMENT,
    `service_type_id` int(20) NOT NULL AUTO_INCREMENT,  PRIMARY KEY  (`service_type_id`))   ;

我做错了什么?对于具有并属于许多关系,从另一个数据库导入灯具的正确方法是什么?

谢谢你!

I have a model Post -> hasAndBelongsToMany -> Tag

For testing, I created the fixtures for each model, for example, a fixture for the model Post looks like this

class PostFixture extends CakeTestFixture {
    var $import = array('model' => 'Post', 'records' => true, 'connection' => 'fixtures');
}

And everything works great for that Model, but when I try to create the fixture for the HABTM relationship, using the same approach doesn’t work:

class PostsTagFixture extends CakeTestFixture {
    var $import = array('model' => 'PostTag', 'records' => true, 'connection' => 'fixtures');
}

The SQL generated by CakePHP is the following

CREATE TABLE `service_types_technicals` (
    `technical_id` int(20) NOT NULL AUTO_INCREMENT,
    `service_type_id` int(20) NOT NULL AUTO_INCREMENT,
    `id` varchar(255) NOT NULL, PRIMARY KEY  (`id`))    ;

Wich is not correct because the table does not have a field named id.

Then, I tried this:

class PostsTagFixture extends CakeTestFixture {
    var $name = 'PostsTag';
    var $import = array('table' => 'posts_tags', 'records' => true, 'connection' => 'fixtures');
}

And again, error, but this time the SQL was:

CREATE TABLE `service_types_technicals` (
    `technical_id` int(20) NOT NULL AUTO_INCREMENT,
    `service_type_id` int(20) NOT NULL AUTO_INCREMENT,  PRIMARY KEY  (`service_type_id`))   ;

What am I doing wrong? What is the correct way to import fixtures from another database for has and belongs to many relationships?

Thank you!

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

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

发布评论

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

评论(2

别忘他 2024-11-04 23:05:41

Mauro Zadunaisky,

正如您所说的 service_types_technicals 没有 id 字段,我猜 CakePHP 会自动从 service_types 推导出 HABTM 到技术,因为两者都是以复数形式书写的名词(CakePHP 约定)。如果这不是您想要的,那么您将被迫更改表的名称以保持在约定范围内。

Mauro Zadunaisky,

as you state that service_types_technicals does not have an id field, I guess that CakePHP automatically deduces a HABTM from service_types to technicals, as both are nouns written in plural (CakePHP convention). If this is not what you had in mind, then you are forced to alter the name of the table to stay within the conventions.

黑白记忆 2024-11-04 23:05:41

问题是habtm关系表中缺少字段id,现已修复

The problem was the missing of the field id in the habtm relation table, it's fixed now

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