yii2-gii: Model Generator does NOT generate relations
This issue has originally been reported by @tsanchev at https://github.com/yiisoft/yii2/issues/12741. Moved here by @cebe.
What steps will reproduce the problem?
I have used this migration to generate the table
public function up()
{
if ($this->db->schema->getTableSchema('products', true) !== null) {
$this->dropTable('products');
}
$this->createTable('products', [
'product_id' => $this->bigPrimaryKey()->unsigned(),
'category_id' => $this->bigInteger()->unsigned()->notNull(),
'brand_id' => $this->bigInteger()->unsigned()->notNull(),
'position' => $this->bigInteger()->unsigned(),
'product_en' => $this->string()->notNull(),
'product_bg' => $this->string()->notNull(),
'product_de' => $this->string()->notNull(),
'product_ru' => $this->string()->notNull(),
'description_en' => $this->text(),
'description_bg' => $this->text(),
'description_de' => $this->text(),
'description_ru' => $this->text(),
'image' => 'LONGBLOB',
'thumbnail' => 'MEDIUMBLOB',
]);
$this->createIndex('idx-products-category_id', 'products', 'category_id');
$this->createIndex('idx-products-brand_id', 'products', 'brand_id');
$this->addForeignKey('fk-products-category_id', 'products', 'category_id', 'categories', 'category_id', 'CASCADE');
$this->addForeignKey('fk-products-brand_id', 'products', 'brand_id', 'brands', 'brand_id', 'CASCADE');
}
and when I use Model Generator and check “[v] Generate Relations from Current Schema” there is no ActiveQuery methods generated
In the relational tables the primary key is generated using $this->bigInteger()->unsigned()
What is the expected result?
In the model class I expect to have this methods generated
/**
* @return \yii\db\ActiveQuery
*/
public function getCategory()
{
return $this->hasOne(Categories::className(), ['category_id' => 'category_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getBrand()
{
return $this->hasOne(Brands::className(), ['brand_id' => 'brand_id']);
}
What do you get instead?
nothing (except rules and attribute labels)
Additional info
| Q | A |
|---|---|
| Yii version | 2.0.10-dev |
| PHP version | 5.6.9 |
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 24 (14 by maintainers)
Fixed in https://github.com/yiisoft/yii2-gii/commit/1238a0c39dc3c39161497e86e9a7d9c3f8c28935. Thank you for a fix @shirase.
Bug appears in 09243fe7fa0d379b842ef3e9582988d9cf8f659b Line https://github.com/yiisoft/yii2-gii/commit/09243fe7fa0d379b842ef3e9582988d9cf8f659b#diff-395f09d34b21668a80e7c68456890857R464 expects array of strings, but https://github.com/yiisoft/yii2-gii/commit/09243fe7fa0d379b842ef3e9582988d9cf8f659b#diff-395f09d34b21668a80e7c68456890857R431 returns empty array.
Issue don’t reproduced if you set components.db.schema.defaultSchema or used not mysql driver.
This issue is not fixed yet. Faced same issue using latest archived version of advanced template on ubuntu 16.04. I was following this video tutorial https://www.youtube.com/watch?v=aSvxwwjaY2U&t=212
It’s happening everytime in my project. The problem is that in yii2-gii\generators\model\Generator.php line 431 returns empty array:
Which is used in generateRelations method (line 464) in a foreach statement. If you return empty array foreach exits immediately, if you return [‘’] then the foreach finds the relations.
The problem is present when using MySql, because yii\db\mysql\Schema doesn’t have determined $defaultSchema. I think the fix shirase@4b81e26 is right. Method getSchemaNames() has to return string[], but in case $db->schema->defaultSchema === null it returns empty array and relations can’t be generated.