Associations with Doctrine PHP

I am trying to associate two objects with Doctrine (PHP).

I have two objects: User and Conversation

One user has many conversations, and the conversation belongs to a maximum of two users (initiated by the conversation and one recipient).

So, in my Doctrine class, I have this in 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'));

And in the User class:

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

For this foreign key, I would like to add something that means "initiator_id OR responder_id".

I think I need a join table to accomplish what I want to do? What would you do in this case?

Thank you for your responses,

Martin

+3
source share
2 answers

, Conversation User manytomany. , 2 , .

" " ( "" ) . , .

, hasMany , ConversationParticipants, conversion_id, user_id , type, (/).

, - ( !)

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);

    }
}

ORM, , , Doctrine ,

, ,,

0

:

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

Source: https://habr.com/ru/post/1768964/


All Articles