Mauro Zadunaisky
Mauro Zadunaisky

Reputation: 828

cakephp testing suite: import fixtures tables for HABTM

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!

Upvotes: 3

Views: 1161

Answers (2)

Mauro Zadunaisky
Mauro Zadunaisky

Reputation: 828

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

Upvotes: 1

benjamin
benjamin

Reputation: 2185

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.

Upvotes: 1

Related Questions