views:

32

answers:

1

Hello

I have modeled two classes with a many to many relationship : User and Conversation, and I can't create a the link between these two classes when I use them :

class User extends Doctrine_Record
{
 public function setTableDefinition() {

         $this->hasColumn('udid', 'string', 255);
      $this->hasColumn('nb_requetes', 'integer', 255);
 }

 public function setUp() {
  $this->actAs('Timestampable');
  $this->hasMany('Conversation as Conversations',
                        array('local'=> 'user_id',
                               foreign'=> 'conversation_id',
                              'refClass' => 'UserConversation'));
 }

}

And

class Conversation extends Doctrine_Record
{
 public function setTableDefinition() {

         $this->setTableName('conversations');

   $this->hasColumn('initiator_id', 'integer', 20);
   $this->hasColumn('responder_id', 'integer', 20);
 }

 public function setUp() {
  $this->actAs('Timestampable');

  $this->hasMany('User as Users',
                            array('local' => 'conversation_id',
                                  'foreign' => 'user_id',
                                  'refClass' => 'UserConversation'));
 }
}

I'm trying to use it by doing something like that :

$user = new User();
$user->save();

$conversation = new Conversation();
$conversation->users[] = $user;
$conversation->save();

But it's not working, my junction table "UserConversation" stays empty... Am I doing it the right way?

Thank you for your answer,

Martin

A: 

Here are the classes required for a many to many. I've adapted them from my own code so the field names are a little different but it should give you an idea of what you need:

class User extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->hasColumn('name', 'string', 255);
    }

    public function setUp()
    {
        parent::setUp();
        $this->hasMany('Conversation as Conversations', array(
            'refClass' => 'UserConversations',
            'local' => 'conversation_id',
            'foreign' => 'user_id'
        ));
    }
}

class Conversation extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->hasColumn('name', 'string', 255);
    }

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

class UserConversations extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->hasColumn('user_id', 'integer', 4, array(
            'type' => 'integer',
            'primary' => true,
            'length' => '4',
        ));
        $this->hasColumn('conversation_id', 'integer', 4, array(
            'type' => 'integer',
            'primary' => true,
            'length' => '4',
        ));
        $this->hasColumn('name', 'string', 255, array(
            'type' => 'string',
            'length' => '255',
        ));
    }

    public function setUp()
    {
        parent::setUp();
    }
}
Flyn San