I am attempting to do a connection of two objects with Doctrine (PHP).

I've two objects : User and Conversation

One user has numerous conversations along with a conversation goes to 2 customers maximum (on initiator from the conversation, and something receiver).

So, during my Doctrine class I've this within the Conversation class :

$this->hasOne('User as Initiator', array('local' => 'initiator_id', 'foreign' => 'id'));
$this->hasOne('User as Responder', array('local' => 'responder_id', 'foreign' => 'id'));

As well as in the consumer class :

$this->hasMany('Conversation as Conversations', array('local'=> 'id', 'foreign'=> ????));

With this foreign key Let me put something which means "initiator_id OR responder_id".

I suppose I want a junction table to complete what I wish to do ? How would you react within this situation ?

Appreciate your solutions,

Martin

my prediction could be this:

$this->hasMany('Conversation as Initiations', array('local'=> 'id', 'foreign'=> 'initiator_id'));
$this->hasMany('Conversation as Responses', array('local'=> 'id', 'foreign'=> 'responder_id'));

It could be easier to re model the relation between your Conversation and User tables to manytomany. because despite the fact that a conversation only have 2 customers, they're still considered several.

In the earlier solution, Getting Two has-many relations with similar table (Conversation) is definitely an over mind. since you will finish up doing two times the job any time you query a a conversation.

My suggestion would be to alter the relation between your conversion and also the user to hasMany and reference them utilizing an association class ConversationParticipants that consists of the conversion_id, user_id as well as an additional association attribute known as type to point the kind of the consumer (Initiator/Responder).

Which means you will finish track of something such as this ( the code isn't examined !)

class Conversation extends Doctrine_Record {

    public function setTableDefinition() {

        // Your Table Fields
    }

    public function setUp() {

        // Your Setup Code

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

}

class User extends BaseUser
{

    public function setTableDefinition() {

        // Your User Fields

    }

    public function setUp()
    {

        // Your Setup Code

        $this->hasMany('Conversation as Conversations', array(
                'local' => 'user_id',
                'foreign' => 'conversation_id',
                'refClass' => 'ConversationParticipants'
            )
        );
    }
}

class ConversationParticipants extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->hasColumn('user_id', 'integer', null, array(
                'primary' => true
            )
        );

        $this->hasColumn('conversation_id', 'integer', null, array(
                'primary' => true
            )
        );

        $this->hasColumn('type', 'string', 255);

    }
}

This is the way i would have implemented it on any ORM, I am unsure though if you're able to use association characteristics in Doctrine or maybe by doing this of molding will act as I am no expert in Doctrine

I Really Hope it was useful ,,,