gii not generating model relations correctly

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

It’s strange. I believe you are doing right. Gii should have created “datasets” relation.

Have you enabled the db schema cache?

Thanks for the reply. I don’t really know what you mean by “Have you enabled the db schema cache?” I’ll check it out on the docs and have a look on Monday.

Sorry for your confusion. I mean this:

http://www.yiiframework.com/doc-2.0/yii-db-connection.html#$enableSchemaCache-detail

Just for double check.

Thanks for the reference…saving me looking for it.

I’m just using the “basic” application template which from what I can see doesn’t have enableSchemaCache enabled. I haven’t added this setting anywhere.

But I just went thru the model creation again for the third time with GII and for some reason it just produced a model with this:

/**

 * @return \yii\db\ActiveQuery


 */


public function getDatasets()


{


    return $this->hasMany(Dataset::className(), ['customer_id' => 'id']);


}

I don’t know what I was doing wrong, nor do I know what I did to fix it!

Sorry for messing the forum around. Thanks softark for your help and patience.

Regards,

bw

Cheers!

You didn’t notice that the checkbox for generating relations was accidentally set to off ? … I really don’t know, but it’s very nice to know that things get going right for you.