tags:

views:

106

answers:

1

im using doctrine and i have set up some test tables to be generated into models:

I want a many-to-many relationship models (3 tables converted into 3 models)

(things are simplified to make the point clear)

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 thought that it would generate these models (from 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')
        );
    }
}

but it generated 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 see, there is no 'refClass' in the 'User' and 'Group' models pointing to the 'UserGroup'. the 'UserGroup' table in this case is just another table from Doctrine's perspective not a typical reference table.

usually if its correct like the first one you can do like this:

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

now you cant cause User is not linked directly to Group, but to UserGroup.

I've checked my table definitions in mysql. They are correct. user_group has 2 columns (primary keys and foreign keys), each one pointing to the primary key in either User or Group.

But i want the standard many-to-many relationship models in Doctrine models.

I'd appreciate some help. I have struggled to figure it out for a half day now.

What is wrong?

Thanks!

+1  A: 
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 have to add hasMany definition to User model.

Vladimir
i know. but everything is generated from the mysql database with generate_models_from_db(). the problem is that doctrine cannot understand that it's a many to many between 3 tables (one of them is a jointable).
never_had_a_name