forked from centurion-project/Centurion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Abstract.php
86 lines (66 loc) · 2.95 KB
/
Abstract.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
82
83
84
85
86
<?php
abstract class Centurion_Traits_Model_DbTable_Abstract extends Centurion_Traits_Abstract
{
protected $_model;
protected $_modelInfo;
protected $_defaultContraint = array('onDelete' => Centurion_Db_Table_Abstract::CASCADE,
'onUpdate' => Centurion_Db_Table_Abstract::CASCADE);
protected $_requiredColumns = array();
public function __construct($model)
{
parent::__construct($model);
$this->_model = $model;
$this->_modelInfo = $this->_model->info();
$this->checkForRequiredColumn();
}
public function checkForRequiredColumn($additionalCols = array())
{
$requiredCols = array_merge($this->_requiredColumns, $additionalCols);
foreach($requiredCols as $col) {
if (!$this->_checkColumnExists($col))
throw new Centurion_Traits_Exception(sprintf('Model %s must have folowing column `%s`', get_class($this->_model), $col));
}
}
protected function _checkColumnExists($columnName, $metadata = array())
{
if (!in_array($columnName, array_keys($this->_modelInfo[Centurion_Db_Table_Abstract::METADATA])))
return false;
foreach ($metadata as $metaName => $metaValue) {
if (!isset($this->_modelInfo[Centurion_Db_Table_Abstract::METADATA][$metaName])
|| $this->_modelInfo[Centurion_Db_Table_Abstract::METADATA][$metaName] !== $metaValue)
return false;
}
return true;
}
protected function _addReferenceMapRule($baseName, $column, $refModelClass, $refColumn = 'id', $constraint = null)
{
$refMap = $this->_referenceMap;
$refMapRule = $this->_genRefRuleName($baseName);
if (null === $constraint) {
$constraint = $this->_defaultContraint;
} else {
$constraint = (array) $constraint;
}
$refMap = array_merge($refMap,
array($refMapRule => array_merge (array(
'columns' => $column,
'refColumns' => 'id',
'refTableClass' => $refModelClass
),
$constraint)
)
);
$this->_referenceMap = $refMap;
return $refMapRule;
}
protected function _addDependentTables($ruleBaseName, $targetModelClass)
{
$ruleName = $this->_genRefRuleName($ruleBaseName);
$this->_dependentTables = array_merge($this->_dependentTables, array($ruleName => $targetModelClass));
return $ruleName;
}
protected function _addManyDependentTables()
{
throw new Centurion_Traits_Exception('Method Centurion_Traits_Model_Abstract::_addManyDependentTables() is not Implemented yet');
}
}