im using doctrine and that i have setup some test tables to become produced into models:

I would like a many-to-many relationship models (3 tables changed into 3 models)

(situations are simplified to create the purpose obvious)

mysql tables:

user:
id INT // primary key
name VARCHAR

group:
id INT // primary key
name VARCHAR

user_group:
user_id INT // primary and foreign key to user.id
group_id INT // primary and foreign key to group.id

i figured it would generate these models (in the documentation):

// User.php

class User extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->hasColumn('id');

        $this->hasColumn('name');
    }

    public function setUp()
    {
        $this->hasMany('Group' as 'Groups', array(
                'refClass' => 'UserGroup',
                'local' => 'user_id',
                'foreign' => 'group_id'
            )
        );
    }
}

// Group.php

class Group extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->hasColumn('id');

        $this->hasColumn('name');
    }

    public function setUp()
    {
        $this->hasMany('User' as 'Users', array(
                'refClass' => 'UserGroup',
                'local' => 'group_id',
                'foreign' => 'user_id'
            )
        );
    }
}

// UserGroup.php

class UserGroup extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->hasColumn('user_id')
        );

        $this->hasColumn('group_id')
        );
    }
}

however it produced this:

// User.php

abstract class BaseUser extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->hasColumn('id');

        $this->hasColumn('name');
    }

    public function setUp()
    {
        $this->hasMany('UserGroup', array(
             'local' => 'id',
             'foreign' => 'user_id'));
    }
}

// Group.php

abstract class BaseGroup extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->hasColumn('id');

        $this->hasColumn('name');
    }

    public function setUp()
    {
        $this->hasMany('UserGroup', array(
             'local' => 'id',
             'foreign' => 'group_id'));
    }
}

// UserGroup.php

abstract class BaseUserGroup extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->hasColumn('user_id');

        $this->hasColumn('group_id');
    }

    public function setUp()
    {
        $this->hasOne('User', array(
             'local' => 'user_id',
             'foreign' => 'id'));

        $this->hasOne('Group', array(
             'local' => 'group_id',
             'foreign' => 'id'));
    }
}

as you can tell, there's no 'refClass' within the 'User' and 'Group' models pointing towards the 'UserGroup'. the 'UserGroup' table within this situation is simply another table from Doctrine's perspective not really a typical reference table.

usually if it is correct like the first that you can do such as this:

$user = new User();
$user->group[1]->name = 'group 1';
$user->group[2]->name = 'group 2';

you now cant cause User isn't linked straight to Group, but to UserGroup.

I have checked my table definitions in mysql. They're correct. user_group has 2 posts (primary secrets and foreign secrets), each one of these pointing towards the primary type in either User or Group.

However i want the conventional many-to-many relationship models in Doctrine models.

I'd appreciate outside assistance. I've battled to decipher it for any half day now.

What's wrong?

Thanks!

Doctrine requires that Many-to-Many relationships be bi-directional. For example: both User must have many Groups and Group  must have many User.

So you've to include hasMany definition to User model.

Doctrine cannot map relations instantly.

You need to by hand assure the relations, due to the fact the constraint that maps the relation differ a great deal between DBMS.

So, generate YAML from DB, update by hand the YAML after which generate the models.

Regards,

Source: http://groups.google.com/group/doctrine-user/browse_thread/thread/1fc3cb7ef39f4343?pli=1