gii & models with relations

Hello,

I’m experiencing a situation that is odd with gii, models & relations.

Here is the test table which has one foreign key.




CREATE TABLE `tbl_utilisateur_cours` (

  `utilisateurPOID` int(11) NOT NULL,

  `coursPOID` int(11) NOT NULL,

  `statutPOID` int(11) NOT NULL,

  `terminerLe` datetime DEFAULT NULL,

  `updateUser` int(11) DEFAULT NULL,

  `updateTimestamp` datetime DEFAULT NULL,

  PRIMARY KEY (`utilisateurPOID`,`coursPOID`),

  KEY `fk_utilisateur` (`utilisateurPOID`),

  CONSTRAINT `fk_utilisateur` FOREIGN KEY (`utilisateurPOID`) REFERENCES `tbl_utilisateur` (`utilisateurPOID`) ON DELETE NO ACTION ON UPDATE NO ACTION

) ENGINE=InnoDB DEFAULT CHARSET=latin1$$



When I generate the model all is well:




class UtilisateurCours extends CActiveRecord

{

...

 /**

     * @return array relational rules.

     */

    public function relations()

    {

        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.

        return array(

            'utilisateurPO' => array(self::BELONGS_TO, 'Utilisateur', 'utilisateurPOID'),

        );

    }

...

}



When I add a second foreign key:




CREATE TABLE `tbl_utilisateur_cours` (

  `utilisateurPOID` int(11) NOT NULL,

  `coursPOID` int(11) NOT NULL,

  `statutPOID` int(11) NOT NULL,

  `terminerLe` datetime DEFAULT NULL,

  `updateUser` int(11) DEFAULT NULL,

  `updateTimestamp` datetime DEFAULT NULL,

  PRIMARY KEY (`utilisateurPOID`,`coursPOID`),

  KEY `fk_utilisateur` (`utilisateurPOID`),

  KEY `fk_cours` (`coursPOID`),

  CONSTRAINT `fk_cours` FOREIGN KEY (`coursPOID`) REFERENCES `tbl_cours` (`coursPOID`) ON

 DELETE NO ACTION ON UPDATE NO ACTION,

  CONSTRAINT `fk_utilisateur` FOREIGN KEY (`utilisateurPOID`) REFERENCES 

`tbl_utilisateur` (`utilisateurPOID`) ON DELETE NO ACTION ON UPDATE NO ACTION

) ENGINE=InnoDB DEFAULT CHARSET=latin1$$



The model is not picking up any foreign keys.




class UtilisateurCours extends CActiveRecord

{

...

/**

     * @return array relational rules.

     */

    public function relations()

    {

        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.

        return array(

        );

    }

...

}



Any insight would be greatly appreciated.

You need to re-generate the model - it doesn’t happen automatically.

When you generate your model, you will be informed that a previous version exists, and get the option to overwrite it.

Did you (re)generate the model after the schema change? :)

jacmoe is saying exactly right here…otherwise manually you have to define that key in your model file…

but i think manually define a new key is better approach rather then generating a model again.because might be you loose all of your custom rules if you define in that model…!!

I even logged out of gii and back to generate the model.

However, I never really clicked generate just preview… so let me try to go thru the generate.

Alright, using either Preview or Generate I still have the missing relation.

So in order to move ahead, I will add the second one manually.

Thanks for the help!