help with generating models from database for many to many in doctrine
        Posted  
        
            by ajsie
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by ajsie
        
        
        
        Published on 2010-04-19T15:40:24Z
        Indexed on 
            2010/04/19
            15:43 UTC
        
        
        Read the original article
        Hit count: 266
        
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 reference table.
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!
© Stack Overflow or respective owner