Hi,
Im a newbie to yii2 so please forgive me if the following is a stupid question. I’m having trouble with GII generating relations correctly. I’ve search all over and can’t seem to find an explanation.
I’ve created the following 3 tables in mysql:
CREATE TABLE IF NOT EXISTS `data_merger`.`customer` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`company_name` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
AUTO_INCREMENT = 4
DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `data_merger`.`dataset` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`customer_id` INT(11) NOT NULL,
PRIMARY KEY (`id`),
INDEX `customer_dataset` (`customer_id` ASC),
CONSTRAINT `customer_dataset`
FOREIGN KEY (`customer_id`)
REFERENCES `data_merger`.`customer` (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `data_merger`.`fileset` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`dataset_id` INT(11) NOT NULL,
PRIMARY KEY (`id`),
INDEX `dataset_fileset` (`dataset_id` ASC),
CONSTRAINT `dataset_fileset`
FOREIGN KEY (`dataset_id`)
REFERENCES `data_merger`.`dataset` (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
So a customer can have many datasets. A dataset is made up of many filesets.
If I run the gii model generation on the dataset table I get the correct relations built in the model:
/**
* @return \yii\db\ActiveQuery
*/
public function getCustomer()
{
return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getFilesets()
{
return $this->hasMany(Fileset::className(), ['dataset_id' => 'id']);
}
Please note the getFilesets. Gii in all its awesomeness has detected that datasets are made up of many filesets.
Now the problem…when I run the Gii model generation over the customer table I just get the following function it the model:
public static function tableName()
{
return 'customer';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['company_name'], 'required'],
[['company_name'], 'string', 'max' => 100]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'company_name' => 'Company Name',
];
}
Notice there are no relation functions like getDatasets(). Using the dataset model as a comparison, I would have expected to get a function like:
/**
* @return \yii\db\ActiveQuery
*/
public function getDatasets()
{
return $this->hasMany(Dataset::className(), [‘customer_id' => 'id']);
}
Is there something that I’m not doing correctly in either my sql or my model generation?
Thanks,
bw