Apologies, as I am new to the MVC structure (and frameworks, for that matter).
After using gii to generate my CRUD, I can tinker and get a given "Create" form to display a dropdown populated by choices from a related one-to-many table. So far so good.
However, I am stymied on how to manipulate a many-to-many relationship in the same manner.
Say I have a genPage table and a categories table joined by a genPageHasCategories junction table.
How do I get the genPage create form to include a section of checkbox options populated by the categories table? (Checkboxes, not a dropdown, as the whole point of m-to-m is to allow a multi-select; also, I'd want to return values from the "name" fields in categories since random numeric ids are not too useful in the UI.)
Of course, I am expecting Yii2 magically to then save those relationships into the GenPageHasCategories junction table.
I know it has something to do with viaTable() and suspect the link() method comes in, but my noob brain just cannot figure out how to format the appropriate code snippets and where to stick them. Controller? Model? View? And which one(s) of each-- the Controller (or Model) of genPage, of categories, or of the joining table?
I see that gii helpfully creates the following in the GenPage.php model:
public function getGenPageHasCategories() { return $this->hasMany(GenPageHasCategory::className(), ['genPage_idgenPage' => 'idgenPage']); } /** * @return \yii\db\ActiveQuery */ public function getCategoryIdcategories() { return $this->hasMany(Category::className(), ['idcategory' => category_idcategory'])->viaTable('genPage_has_category' ['genPage_idgenPage' => 'idgenPage']); }
I tried to find a handy extension or widget, but none seems to be up and running yet. (Though giiant helpfully points to such relationships with a handy dropdown on the view page, it still keeps the CRUD for the junction tables separate (and unintelligibly populates them with ids rather than the human-readable names. It also generates subset folders in controllers and models, confusing this noob even further.)