I have been working on a project to develop MIS for university academic staff
I want to use seperate tables for each academic year to store lecture details (lecturers assigned to each subject)
I have develop the model class as follows
Troble is with tableName;
following works well
new Assignment('2012/2013)->tableName();//output:lecture2012_2013 :correct
But
Assignment::model(‘2012/2013’)->find()->tableName();
//output:lecture2013_2014 :default name
//:wrong, but correctly fetches data
Can anybody help … please?
5770
part of the model class:
/* start_of_dynamic_tables_code */
private $tableName = 'lecture'; // <=default value
private static $_models = array();
private $_md;
public function __construct($tableName = null, $scenario = 'insert') {
if ($tableName === null || $tableName === '') {
$criteria = new CDbCriteria();
$criteria->select = 'MAX(Year) as Year';
$tableName = AcademicCalendar::model()->find($criteria)->Year;
$this->tableName = $this->tableName . $tableName;
}
else{
$tableName=str_replace('lecture','',$tableName);
$this->tableName ='lecture'.$tableName;
}
$this->tableName= str_replace('/', '_', $this->tableName);
parent::__construct($scenario);
}
public static function model($tableName = false, $className = __CLASS__) {
$tableName= str_replace('/', '_', $tableName);
$tableName=str_replace('lecture','',$tableName);
if ($tableName === null)
$className = null;
// this string will save internal CActiveRecord functionality
if (!$tableName)
return parent::model($className);
if($tableName!==null || $tableName){
$tableName ='lecture'.$tableName;
}
if (isset(self::$_models[$tableName . $className]))
return self::$_models[$tableName . $className];
else {
$model = self::$_models[$tableName . $className] = new $className(null);
//$model = parent::model();
$model->tableName = $tableName;
$model->_md = new CActiveRecordMetaData($model);
$model->attachBehaviors($model->behaviors());
return $model;
}
}
public function tableName() {
return $this->tableName;
}
/**
* Returns the meta-data for this AR
* @return CActiveRecordMetaData the meta for this AR class.
*/
public function getMetaData() {
if ($this->_md === null)
$this->_md = new CActiveRecordMetaData($this);
return $this->_md;
}
public function refreshMetaData() {
$finder = static::model($this->tableName());
$finder->_md = new CActiveRecordMetaData($finder);
if ($this !== $finder)
$this->_md = $finder->_md;
}
/* end_of_dynamic_tables_code */