arogachev/yii2-many-to-many

multiple relations in the same model

Opened this issue · 9 comments

Im having an issue, when i want to incorporate 2 relations of arogachev instance.
My model is called Client.
It has m2m relationship with other 2 models via pivot tables.
client <-> clientCompanies <-> company
client <-> clientSheets <-> sheet
this is my config in behaviors of Client model

'class' => ManyToManyBehavior::className(),
'relations' => [
[
'name' => 'clientCompanies',
'editableAttribute' => 'companies',
'fillingRoute' => 'client/update',
],
[
'name' => 'clientSheets',
'editableAttribute' => 'sheets',
'fillingRoute' => 'sheet/update',
],
],

Note that clientCompanies relationship works FINE.

Please add the according relations.

Client model

public function behaviors()
   {
       return [
           [
               'class' => ManyToManyBehavior::className(),
               'relations' => [
                   [
                       'name' => 'clientCompanies',
                       'editableAttribute' => 'companies',
                       'fillingRoute' => 'client/update',
                   ],
                   [
                       'name' => 'clientKads',
                       'editableAttribute' => 'kads',
                       'fillingRoute' => 'kad/update',
                   ],
               ],
           ],
       ];
    }

.....

 public function rules()
    {
        return [
            ['companies', 'safe'],
            ['kads', 'safe'],
            [['name'], 'string'],
            [['taxnum'], 'integer'],
            [['notes'], 'string'],
            [['email'], 'string'],
            [['phone'], 'string', 'max' => 13],
            [['address'], 'string', 'max' => 80],
        ];
    }

.....

(relations as requested below)

  /**
    * @return \yii\db\ActiveQuery
    */
   public function getClientCompanies()
   {
       return $this->hasMany(Company::className(), ['id' => 'company_id'])
           ->viaTable('clientCompanies', ['client_id' => 'id'])
           ->orderBy('name');
   }

    /**
    * @return \yii\db\ActiveQuery
    */
   public function getClientKads()
   {
       return $this->hasMany(Kad::className(), ['id' => 'kad_id'])
           ->viaTable('clientKads', ['client_id' => 'id'])
           ->orderBy('name');
   }


With this setup, when i create a CLIENT, both relationships are saved in their pivot tables.
BUT, when i get back to update a client (update action) the client model returns only the IDS of the first relationship and the second one is an empty array. The weird is that both my pivot tables have the right data from the create action. So the issue is that the model Client not fetching the keys from clientKads pivot.

I also have

   /**
    * @var array
    */
    public $companies = [];
    /**
    * @var array
    */
    public $kads = [];

on my Client model

So you want to edit them both on one page, right?

Yes that's right. My goal is a client entity with two m2m relationships with one page edit. Is there a way to achieve that functionality with the current code base?

Yes, just add this page route to both of relations.

What a mistake! Obvious enough for someone to see it :)
Keep up the great work. Your extension is a great piece of code.Thanks for your help.

It's OK. We are all making mistakes sometimes. 😄 Thanks!