Answer the question
In order to leave comments, you need to log in
Yii2 many-to-many saving via link(), how to organize?
I can’t find a normal example or a detailed description in the documentation, but I can’t understand it by parsing the code :) I
see the same example everywhere:
We define relationships in models
public function getMarkets() {
return $this->hasMany(Market::className(), ['id' => 'market_id'])
->viaTable('tbl_user_market', ['user_id' => 'id']);
}
public function getUsers() {
return $this->hasMany(User::className(), ['id' => 'user_id'])
->viaTable('tbl_user_market', ['market_id' => 'id']);
}
$user = new User;
$user->name = 'Foo';
$user->save();
$market = new Market;
$market->name = 'Bar';
$market->save();
$user->link('markets', $market);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question