/
Dynamic.php
81 lines (71 loc) · 2.16 KB
/
Dynamic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
* Dynamic model usable for multiple tables.
*/
class Dynamic extends CActiveRecord
{
private static $_models = array();
private $_md; // meta data
private $tableName;
/**
* @param string $scenario
* @param string $tableName
*/
public function __construct($scenario = 'insert', $tableName = null)
{
if (!isset($tableName))
{
//Yii::trace('sTableName missing.');
throw new Exception('$tableName missing.');
}
$this->tableName = $tableName;
parent::__construct($scenario);
}
protected function instantiate($attributes)
{
$class = get_class($this);
$model = new $class(null, $this->tableName);
return $model;
}
/**
* We have a custom implementation here since the parents' implementation
* does not create a new model for each table name.
* @param type $className
* @return Plugin
*/
public static function model($className = __CLASS__, $tableName = null)
{
if (isset($tableName))
{
if (!isset(self::$_models[$tableName]))
{
$model = self::$_models[$tableName] = new $className(null, $tableName);
$model->_md = new CActiveRecordMetaData($model);
$model->attachBehaviors($model->behaviors());
}
return self::$_models[$tableName];
}
else
{
throw new Exception('$tableName missing.');
}
}
/**
* Gets the tablename for the current model.
*/
public function tableName() {
return $this->tableName;
}
/**
* Override
* @return CActiveRecordMetaData the meta for this AR class.
*/
public function getMetaData()
{
if($this->_md!==null)
return $this->_md;
else
return $this->_md = self::model(get_class($this), $this->tableName)->_md;
}
}
?>