Here someone explained that setting a 'primary' attribute for any row in your Model will override Doctrine_Manager's ATTR_DEFAULT_IDENTIFIER_OPTIONS attribute:
http://stackoverflow.com/questions/2040675/how-do-you-override-a-constant-in-doctrines-models
This works, however if you have a many to many relation whereby the intermediate table is created, even if you have set both columns in the intermediate to primary an error still results when Doctrine tries to place an index on the nonexistant 'id' column upon table creation. Here's my code:
//Bootstrap
// set the default primary key to be named 'id', integer, 4 bytes
Doctrine_Manager::getInstance()->setAttribute(
Doctrine_Core::ATTR_DEFAULT_IDENTIFIER_OPTIONS,
array('name' => 'id', 'type' => 'integer', 'length' => 4));
//User Model
class User extends Doctrine_Record
{
public function setTableDefinition()
{
$this->setTableName('users');
}
public function setUp()
{
$this->hasMany('Role as roles', array(
'local' => 'id',
'foreign' => 'user_id',
'refClass' => 'UserRole',
'onDelete' => 'CASCADE'
));
}
}
//Role Model
class Role extends Doctrine_Record
{
public function setTableDefinition()
{
$this->setTableName('roles');
}
public function setUp()
{
$this->hasMany('User as users', array(
'local' => 'id',
'foreign' => 'role_id',
'refClass' => 'UserRole'
));
}
}
//UserRole Model
class UserRole extends Doctrine_Record
{
public function setTableDefinition()
{
$this->setTableName('roles_users');
$this->hasColumn('user_id', 'integer', 4, array('primary'=>true));
$this->hasColumn('role_id', 'integer', 4, array('primary'=>true));
}
}
Resulting error:
SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'id' doesn't exist in table. Failing Query: "CREATE TABLE roles_users (user_id INT UNSIGNED NOT NULL, role_id INT UNSIGNED NOT NULL, INDEX id_idx (id), PRIMARY KEY(user_id, role_id)) ENGINE = INNODB". Failing Query: CREATE TABLE roles_users (user_id INT UNSIGNED NOT NULL, role_id INT UNSIGNED NOT NULL, INDEX id_idx (id), PRIMARY KEY(user_id, role_id)) ENGINE = INNODB
I'm creating my tables using
Doctrine::createTablesFromModels();