Hello world,
I apologize for my English but I’m French and I have not mastered much English !
Recently I had a problem to create a model with a join three tables. For more details click here
Here is the schema of my database :
And are the model of tables genered by yiic.
I have renamed "animes_has_animes_genres.php" to "AnimesHasAnimesGenres.php" otherwise I get nothing!
AnimesHasAnimesGenres.php
<?php
class animes_has_animes_genres extends CActiveRecord
{
/**
* The followings are the available columns in table 'animes_has_animes_genres':
* @var integer $animes_id
* @var integer $animes_genres_id
* @var integer $id
*/
/**
* Returns the static model of the specified AR class.
* @return animes_has_animes_genres the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'animes_has_animes_genres';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('animes_id, animes_genres_id, id', 'safe', 'on'=>'search'),
);
}
/**
* @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(
'animes' => array(self::BELONGS_TO, 'Animes', 'animes_id'),
'animes_genres' => array(self::BELONGS_TO, 'AnimesGenres', 'animes_genres_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'animes_id' => 'Animes',
'animes_genres_id' => 'Animes Genres',
'id' => 'Id',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('animes_id',$this->animes_id);
$criteria->compare('animes_genres_id',$this->animes_genres_id);
$criteria->compare('id',$this->id);
return new CActiveDataProvider('animes_has_animes_genres', array(
'criteria'=>$criteria,
));
}
}
animes.php
<?php
class animes extends CActiveRecord
{
/**
* The followings are the available columns in table 'animes':
* @var integer $id
* @var string $titre
* @var string $titre_original
* @var string $annee_production
* @var string $studio
* @var string $duree
* @var string $synopsis
* @var integer $nb_episode_dispo
* @var string $fansub
* @var integer $episode_ddl
*/
/**
* Returns the static model of the specified AR class.
* @return animes the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'animes';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('nb_episode_dispo, episode_ddl', 'numerical', 'integerOnly'=>true),
array('titre, titre_original', 'length', 'max'=>150),
array('annee_production, studio', 'length', 'max'=>45),
array('duree', 'length', 'max'=>7),
array('fansub', 'length', 'max'=>250),
array('synopsis', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, titre, titre_original, annee_production, studio, duree, synopsis, nb_episode_dispo, fansub, episode_ddl', 'safe', 'on'=>'search'),
);
}
/**
* @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(
'animes_ddls' => array(self::HAS_MANY, 'AnimesDdl', 'animes_id'),
'animes_has_animes_auteurs' => array(self::HAS_MANY, 'AnimesHasAnimesAuteurs', 'animes_id'),
'animes_has_animes_genres' => array(self::HAS_MANY, 'AnimesHasAnimesGenres', 'animes_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'Id',
'titre' => 'Titre',
'titre_original' => 'Titre Original',
'annee_production' => 'Annee Production',
'studio' => 'Studio',
'duree' => 'Duree',
'synopsis' => 'Synopsis',
'nb_episode_dispo' => 'Nb Episode Dispo',
'fansub' => 'Fansub',
'episode_ddl' => 'Episode Ddl',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('titre',$this->titre,true);
$criteria->compare('titre_original',$this->titre_original,true);
$criteria->compare('annee_production',$this->annee_production,true);
$criteria->compare('studio',$this->studio,true);
$criteria->compare('duree',$this->duree,true);
$criteria->compare('synopsis',$this->synopsis,true);
$criteria->compare('nb_episode_dispo',$this->nb_episode_dispo);
$criteria->compare('fansub',$this->fansub,true);
$criteria->compare('episode_ddl',$this->episode_ddl);
return new CActiveDataProvider('animes', array(
'criteria'=>$criteria,
));
}
}
animes_genres.php
<?php
class animes_genres extends CActiveRecord
{
/**
* The followings are the available columns in table 'animes_genres':
* @var integer $id
* @var string $titre
* @var string $description
*/
/**
* Returns the static model of the specified AR class.
* @return animes_genres the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'animes_genres';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('titre', 'length', 'max'=>45),
array('description', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, titre, description', 'safe', 'on'=>'search'),
);
}
/**
* @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(
'animes_has_animes_genres' => array(self::HAS_MANY, 'AnimesHasAnimesGenres', 'animes_genres_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'Id',
'titre' => 'Titre',
'description' => 'Description',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('titre',$this->titre,true);
$criteria->compare('description',$this->description,true);
return new CActiveDataProvider('animes_genres', array(
'criteria'=>$criteria,
));
}
}
When i use :
<?php
$animes = animes::model()->with('animes_has_animes_genres','animes_genres')->findAll();
foreach($animes as $anime ) {
echo 'id = '.$anime->id.'<br/>';
echo 'Titre = '.$anime->titre.'<br/>';
}
?>
I have a white page, no message, nothing !
Thanks for your help !