3つのテーブルを用意します。
1. b_bs
2. c_cs
3. b_bs_cs
B (b_bs) habtm C (c_cs) の関係にあるとします。
b_bs, c_cs のように、両テーブルの prefix を別々にします。
joinTable は b_bs に合わせておきます。
b_bs
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
c_cs
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
b_bs_cs
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| b_id | int(11) | NO | | NULL | |
| c_id | int(11) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
DATABASE_CONFIG を以下のように設定します。
// config/database.php
class DATABASE_CONFIG
{
public $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'database' => 'test_habtm',
'prefix' => 'b_', ................................. (1)
'encoding' => 'utf8'
);
public $sub = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'admin1',
'password' => 'admin1',
'database' => 'test_habtm',
'prefix' => 'c_', ................................. (2)
'encoding' => 'utf8'
);
}
(1),(2) のように prefix が異ならせておきます。
この設定で、それぞれの index action にアクセスしてみると、
http://localhost/sandbox/test_habtm/bs/index ............... Error
http://localhost/sandbox/test_habtm/cs/index ............... OK
となります。Error 内容は、
| Missing Database Table
| Error: Database table c_bs_cs for model BsC was not found.
joinTable の prefix は 'b_' としているのに、何故 prefix 'c_' でテーブルにアクセスするのか?