Answer the question
In order to leave comments, you need to log in
How to display one-to-many and mamy-to-many relationship in gii code generator in yii framework CRUD operations?
Good day! I searched a lot of sites, but did not find how to display one-to-many and many-to-many relationships. I only found how to implement them during migration, so that the tables in the database are linked by primary and foreign keys, but I did not find its value instead of the foreign key. And I also have a task - to implement this without sculpting cumbersome constructions from htlm and php code in the form of foreach and echo for each page, but all using the framework (there is a suspicion that, probably, you need to add a few lines of code in the model, or in the controller ). Actually the task itself. There are 3 tables. product(id, name, id_category, id_country, s_description, description, price). country(id, country). category(id, category). The product table has foreign keys to the country (id_country => id) and category (id_country => id) tables.
Migrations:
category table
public function safeUp()
{
$this->createTable('category', [
'id' => $this->primaryKey(),
'category' => $this->string(),
]);
$this->alterColumn('category','id',$this->smallInteger(8).'NOT NULL AUTO_INCREMENT');
}
public function safeDown()
{
$this->dropTable('category');
}
public function safeUp()
{
$this->createTable('country', [
'id' => $this->primaryKey(),
'country' => $this->string(),
]);
$this->alterColumn('country','id',$this->smallInteger(8).'NOT NULL AUTO_INCREMENT');
}
public function safeDown()
{
$this->dropTable('country');
}
public function safeUp()
{
$this->createTable('product', [
'id' => $this->primaryKey(),
'name' => $this->string(),
'id_category' => $this->smallInteger(8),
'id_country' => $this->smallInteger(8),
's_description' => $this->string(),
'description' => $this->string(),
'price' => $this->integer(),
]);
$this->alterColumn('product','id',$this->smallInteger(8).'NOT NULL AUTO_INCREMENT');
$this->addForeignKey(
'product_to_category',
'product',
'id_category',
'category',
'id',
'CASCADE'
);
$this->addForeignKey(
'product_to_country',
'product',
'id_country',
'country',
'id',
'CASCADE'
);
}
public function safeDown()
{
$this->dropTable('product');
}
...
public function getProducts()
{
return $this->hasMany(Product::className(), ['id_category' => 'id']);
}
...
public function getProducts()
{
return $this->hasMany(Product::className(), ['id_country' => 'id']);
}
...
/**
* @return \yii\db\ActiveQuery
*/
public function getCategory()
{
return $this->hasOne(Category::className(), ['id' => 'id_category']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCountry()
{
return $this->hasOne(Country::className(), ['id' => 'id_country']);
}
...
public function actionIndex()
{
$searchModel = new CategorySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
...
...
public function actionIndex()
{
$searchModel = new CountrySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
...
...
public function actionIndex()
{
$searchModel = new ProductSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
...
Answer the question
In order to leave comments, you need to log in
Good afternoon.
This is redundant in all migrations
If in the GridView you want to display not the id, but the name of the category, for example, then in the value you specify this through the link
// for hasOne()
'value' => function($model){
return $model->country->name ?? null;
}
// for hasMany()
'value' => function($model){
return implode(',', ArrayHelper::map($model->country, 'id', 'name'));
}
'value' => 'country.name'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question