Setting up Table Prefixes
The final step of the Installation Instructions asks you to make sure you set the dbprefix in your database settings to an empty string. The reason for this is because DataMapper has its own way of managing prefixing, giving some added flexibility as well.
If you don't plan on using prefixes, go right ahead and skip to the Relationship Types section.
Prefix Settings
There's a few ways you can define your prefixes, with the use of the $prefix and $join_prefix class variables.
- $prefix - If set, will require all tables (both normal and joining tables) to have this prefix.
- $join_prefix - If set, will require all joining tables to have this prefix (overrides $prefix).
To make all your DataMapper models use the same prefixes, I recommend setting the prefixes in the DataMapper config, rather than setting the same prefixes in all of them. If you do this, you can still override the prefix for individual models by setting the prefix within them.
Prefix Only
Let's go with the assumption that we've set our prefix up like so, and it applies to all of our models:
var $prefix = "ci_"; var $join_prefix = "";
Using the first group of tables from the Database Tables section, those being countries, countries_users and users, this is how they would be changed to work with the above set prefix:
ci_countries
id | code | name |
---|---|---|
12 | AM | Armenia |
13 | AW | Aruba |
14 | AU | Australia |
15 | AT | Austria |
ci_countries_users
id | country_id | user_id |
---|---|---|
1 | 14 | 7 |
1 | 12 | 8 |
ci_users
id | username | password | |
---|---|---|---|
7 | Foo | ec773c1da6f96b0265d76fa0a53db697e66a8eea | foo@bar.com |
8 | Baz | 383f27f548397ea123ec444505ef4c7cd993dbf6 | baz@qux.com |
You'll notice that only the table names were affected, including the joining table's name, and that prefixing has no affect on the field names.
Both Prefixes
Let's change our prefixes so we're setting a different prefix for our joining tables:
var $prefix = "normal_"; var $join_prefix = "join_";
normal_countries
id | code | name |
---|---|---|
12 | AM | Armenia |
13 | AW | Aruba |
14 | AU | Australia |
15 | AT | Austria |
join_countries_users
id | country_id | user_id |
---|---|---|
1 | 14 | 7 |
1 | 12 | 8 |
normal_users
id | username | password | |
---|---|---|---|
7 | Foo | ec773c1da6f96b0265d76fa0a53db697e66a8eea | foo@bar.com |
8 | Baz | 383f27f548397ea123ec444505ef4c7cd993dbf6 | baz@qux.com |
Join Prefix Only
Now let's change it so we're only prefixing our joining table's, leaving our normal tables without a prefix:
var $prefix = ""; var $join_prefix = "join_";
countries
id | code | name |
---|---|---|
12 | AM | Armenia |
13 | AW | Aruba |
14 | AU | Australia |
15 | AT | Austria |
join_countries_users
id | country_id | user_id |
---|---|---|
1 | 14 | 7 |
1 | 12 | 8 |
users
id | username | password | |
---|---|---|---|
7 | Foo | ec773c1da6f96b0265d76fa0a53db697e66a8eea | foo@bar.com |
8 | Baz | 383f27f548397ea123ec444505ef4c7cd993dbf6 | baz@qux.com |
Combination Prefix
As mentioned earlier, you can set specific prefixes for individual models. If we had the following prefixes setup to apply to all of our models, by setting it in the DataMapper config:
var $prefix = "normal_"; var $join_prefix = "join_";
And then had the following in our users model:
var $prefix = "special_";
Important: All joining tables must use the same prefix, so you should not override the $join_prefix with a different value if it is already set.
The tables would end up as:
normal_countries
id | code | name |
---|---|---|
12 | AM | Armenia |
13 | AW | Aruba |
14 | AU | Australia |
15 | AT | Austria |
join_countries_users
id | country_id | user_id |
---|---|---|
1 | 14 | 7 |
1 | 12 | 8 |
special_users
id | username | password | |
---|---|---|---|
7 | Foo | ec773c1da6f96b0265d76fa0a53db697e66a8eea | foo@bar.com |
8 | Baz | 383f27f548397ea123ec444505ef4c7cd993dbf6 | baz@qux.com |