Made a few small modifications - but haven’t tested though.
<?php
/**
* This is the model class for table "tbl_media".
*
* The followings are the available columns in table 'tbl_media':
* @property integer $id
* @property string $name
* @property string $path
* @property integer $user_id
* @property string $create_time
*
* The followings are the available model relations:
* @property Genre[] $genres
*/
class Media extends CActiveRecord
{
public $file;
public $genreIds = array();
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Media the static model class
*/
public function behaviors()
{
return array(
'CAdvancedArBehavior' => array(
'class' => 'application.extensions.CAdvancedArBehavior'));
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'tbl_media';
}
/**
* @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('name, path, genreIds, user_id', 'required'),
array('user_id', 'numerical', 'integerOnly'=>true),
array('name', 'length', 'max'=>100),
array('path', 'length', 'max'=>150),
array('create_time, genreIds', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, name, path, genreIds, user_id, create_time', '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(
'genres' => array(self::MANY_MANY, 'Genre', 'tbl_media_genre(mediaId, genreId)'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'name' => 'Name',
'path' => 'Path',
'user_id' => 'User',
'create_time' => 'Create Time',
'genreIds' => 'Genres',
);
}
/**
* 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('name',$this->name,true);
$criteria->compare('path',$this->path,true);
$criteria->compare('user_id',$this->user_id);
$criteria->compare('create_time',$this->create_time,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
public function getGenreOptions()
{
$uglyGenres = CHtml::listData(Genre::model()->findAll(), 'id', 'genre_name');
$beautifulGenres = array();
foreach($uglyGenres as $id => $name)
{
$beautifulGenres[] = ucfirst($name);
}
return $beautifulGenres;
}
public function afterFind()
{
if(!empty($this->genres))
{
foreach($this->genres as $n => $genre)
$this->genreIds[] = $genre->id;
}
parent::afterFind();
}
}