Changeset 377 for trunk/Common/Model.php
- Timestamp:
- Jan 20, 2012, 3:05:24 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Common/Model.php
r373 r377 31 31 var $Properties; 32 32 var $System; 33 var $Module; 34 var $Installed; 33 35 34 36 function __construct($Database, $System) … … 36 38 $this->Database = &$Database; 37 39 $this->System = &$System; 38 $this->AddPropertyDateTime('TimeCreate'); 39 $this->AddPropertyOneToMany('UserCreate', 'User'); 40 $this->AddPropertyDateTime('TimeModify'); 41 $this->AddPropertyOneToMany('UserModify', 'User'); 42 $this->AddPropertyOneToMany('Module', 'Module'); 43 } 44 45 function AddPropertyDateTime($Name) 46 { 47 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyDateTime); 48 } 49 50 function AddPropertyDate($Name) 51 { 52 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyDate); 53 } 54 55 function AddPropertyTime($Name) 56 { 57 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyTime); 58 } 59 60 function AddPropertyOneToMany($Name, $TargetModel) 61 { 62 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyOneToMany, 'TargetModel' => $TargetModel); 63 } 64 65 function AddPropertyText($Name) 66 { 67 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText); 68 } 69 70 function AddPropertyText8($Name) 71 { 72 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText8); 73 } 74 75 function AddPropertyText16($Name) 76 { 77 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText16); 78 } 79 80 function AddPropertyText24($Name) 81 { 82 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText24); 83 } 84 85 function AddPropertyText32($Name) 86 { 87 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText32); 88 } 89 90 function AddPropertyString($Name) 91 { 92 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyString); 93 } 94 95 function AddPropertyInteger($Name) 96 { 97 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger); 98 } 99 100 function AddPropertyInteger8($Name) 101 { 102 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger8); 103 } 104 105 function AddPropertyInteger16($Name) 106 { 107 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger16); 108 } 109 110 function AddPropertyInteger24($Name) 111 { 112 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger24); 113 } 114 115 function AddPropertyInteger32($Name) 116 { 117 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger32); 118 } 119 120 function AddPropertyInteger64($Name) 121 { 122 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger64); 123 } 124 125 function AddPropertyFloat($Name) 126 { 127 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyFloat); 128 } 129 130 function AddPropertyDouble($Name) 131 { 132 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyDouble); 133 } 134 135 function AddPropertyBoolean($Name) 136 { 137 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyBoolean); 40 $this->AddPropertyOneToMany('ItemOwnerModule', 'SystemModule', true); 41 $this->AddPropertyDateTime('ItemTimeCreate', true); 42 $this->AddPropertyOneToMany('ItemUserCreate', 'User', true); 43 $this->AddPropertyDateTime('ItemTimeModify', true); 44 $this->AddPropertyOneToMany('ItemUserModify', 'User', true); 45 } 46 47 function AddPropertyDateTime($Name, $Null = false) 48 { 49 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyDateTime, 50 'Null' => $Null); 51 } 52 53 function AddPropertyDate($Name, $Null = false) 54 { 55 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyDate, 56 'Null' => $Null); 57 } 58 59 function AddPropertyTime($Name, $Null = false) 60 { 61 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyTime, 62 'Null' => $Null); 63 } 64 65 function AddPropertyOneToMany($Name, $TargetModel, $Null = false) 66 { 67 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyOneToMany, 68 'TargetModel' => $TargetModel, 'Null' => $Null); 69 } 70 71 function AddPropertyText($Name, $Null = false) 72 { 73 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText, 74 'Null' => $Null); 75 } 76 77 function AddPropertyText8($Name, $Null = false) 78 { 79 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText8, 80 'Null' => $Null); 81 } 82 83 function AddPropertyText16($Name, $Null = false) 84 { 85 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText16, 86 'Null' => $Null); 87 } 88 89 function AddPropertyText24($Name, $Null = false) 90 { 91 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText24, 92 'Null' => $Null); 93 } 94 95 function AddPropertyText32($Name, $Null = false) 96 { 97 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText32, 98 'Null' => $Null); 99 } 100 101 function AddPropertyString($Name, $Null = false) 102 { 103 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyString, 104 'Null' => $Null); 105 } 106 107 function AddPropertyInteger($Name, $Null = false) 108 { 109 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger, 110 'Null' => $Null); 111 } 112 113 function AddPropertyInteger8($Name, $Null = false) 114 { 115 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger8, 116 'Null' => $Null); 117 } 118 119 function AddPropertyInteger16($Name, $Null = false) 120 { 121 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger16, 122 'Null' => $Null); 123 } 124 125 function AddPropertyInteger24($Name, $Null = false) 126 { 127 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger24, 128 'Null' => $Null); 129 } 130 131 function AddPropertyInteger32($Name, $Null = false) 132 { 133 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger32, 134 'Null' => $Null); 135 } 136 137 function AddPropertyInteger64($Name, $Null = false) 138 { 139 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger64, 140 'Null' => $Null); 141 } 142 143 function AddPropertyFloat($Name, $Null = false) 144 { 145 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyFloat, 146 'Null' => $Null); 147 } 148 149 function AddPropertyDouble($Name, $Null = false) 150 { 151 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyDouble, 152 'Null' => $Null); 153 } 154 155 function AddPropertyBoolean($Name, $Null = false) 156 { 157 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyBoolean, 158 'Null' => $Null); 138 159 } 139 160 140 function AddPropertyManyToMany($TargetModel, $Relation, $Colum Own, $ColumnTarget)161 function AddPropertyManyToMany($TargetModel, $Relation, $ColumnOwn, $ColumnTarget, $Null = false) 141 162 { 142 163 $this->Properties[] = array('Type' => PropertyManyToMany, 143 'TargetModel' => $TargetModel, ' Relation' => $Relation, 'ColumnOwn' => $ColumnOwn,144 'ColumnTarget' => $ColumnTarget );164 'TargetModel' => $TargetModel, 'Name' => $Relation, 'ColumnOwn' => $ColumnOwn, 165 'ColumnTarget' => $ColumnTarget, 'Null' => $Null); 145 166 } 146 167 147 168 function Install() 148 169 { 170 if($this->Installed) return; 171 $this->Installed = true; 149 172 $this->Database->insert('SystemModel', array('Name' => $this->Name, 'Module' => $this->Module->Id)); 150 173 $this->Id = $this->Database->insert_id; … … 156 179 $this->Database->insert('SystemModelProperty', array('Name' => $Property['Name'], 157 180 'Model' => $this->Id, 'Type' => $Property['Type'])); 181 if($Property['Null']) $Null = 'NULL'; 182 else $Null = 'NOT NULL'; 158 183 if($Property['Type'] == PropertyDateTime) 159 $Query .= '`'.$Property['Name'].'` DATETIME NOT NULL,';184 $Query .= '`'.$Property['Name'].'` DATETIME '.$Null.','; 160 185 else if($Property['Type'] == PropertyDate) 161 $Query .= '`'.$Property['Name'].'` DATE NOT NULL,';186 $Query .= '`'.$Property['Name'].'` DATE '.$Null.','; 162 187 else if($Property['Type'] == PropertyTime) 163 $Query .= '`'.$Property['Name'].'` TIME NOT NULL,';188 $Query .= '`'.$Property['Name'].'` TIME '.$Null.','; 164 189 else if($Property['Type'] == PropertyString) 165 $Query .= '`'.$Property['Name'].'` VARCHAR(255) COLLATE utf8_general_ci NOT NULL,';190 $Query .= '`'.$Property['Name'].'` VARCHAR(255) COLLATE utf8_general_ci '.$Null.','; 166 191 else if($Property['Type'] == PropertyText8) 167 $Query .= '`'.$Property['Name'].'` TINYTEXT COLLATE utf8_general_ci NOT NULL,';192 $Query .= '`'.$Property['Name'].'` TINYTEXT COLLATE utf8_general_ci '.$Null.','; 168 193 else if($Property['Type'] == PropertyText16) 169 $Query .= '`'.$Property['Name'].'` TEXT COLLATE utf8_general_ci NOT NULL,';194 $Query .= '`'.$Property['Name'].'` TEXT COLLATE utf8_general_ci '.$Null.','; 170 195 else if($Property['Type'] == PropertyText24) 171 $Query .= '`'.$Property['Name'].'` MEDIUMTEXT COLLATE utf8_general_ci NOT NULL,';196 $Query .= '`'.$Property['Name'].'` MEDIUMTEXT COLLATE utf8_general_ci '.$Null.','; 172 197 else if($Property['Type'] == PropertyText32) 173 $Query .= '`'.$Property['Name'].'` LONGTEXT COLLATE utf8_general_ci NOT NULL,';198 $Query .= '`'.$Property['Name'].'` LONGTEXT COLLATE utf8_general_ci '.$Null.','; 174 199 else if($Property['Type'] == PropertyInteger8) 175 $Query .= '`'.$Property['Name'].'` TINYINT(4) NOT NULL,';200 $Query .= '`'.$Property['Name'].'` TINYINT(4) '.$Null.','; 176 201 else if($Property['Type'] == PropertyInteger16) 177 $Query .= '`'.$Property['Name'].'` SMALLINT(6) NOT NULL,';202 $Query .= '`'.$Property['Name'].'` SMALLINT(6) '.$Null.','; 178 203 else if($Property['Type'] == PropertyInteger24) 179 $Query .= '`'.$Property['Name'].'` MEDIUMINT(9) NOT NULL,';204 $Query .= '`'.$Property['Name'].'` MEDIUMINT(9) '.$Null.','; 180 205 else if($Property['Type'] == PropertyInteger32) 181 $Query .= '`'.$Property['Name'].'` INT(11) NOT NULL,';206 $Query .= '`'.$Property['Name'].'` INT(11) '.$Null.','; 182 207 else if($Property['Type'] == PropertyInteger64) 183 $Query .= '`'.$Property['Name'].'` BIGINT(20) NOT NULL,';208 $Query .= '`'.$Property['Name'].'` BIGINT(20) '.$Null.','; 184 209 else if($Property['Type'] == PropertyBoolean) 185 $Query .= '`'.$Property['Name'].'` BOOLEAN NOT NULL,';210 $Query .= '`'.$Property['Name'].'` BOOLEAN '.$Null.','; 186 211 else if($Property['Type'] == PropertyFloat) 187 $Query .= '`'.$Property['Name'].'` FLOAT NOT NULL,';212 $Query .= '`'.$Property['Name'].'` FLOAT '.$Null.','; 188 213 else if($Property['Type'] == PropertyDouble) 189 $Query .= '`'.$Property['Name'].'` DOUBLE NOT NULL,';214 $Query .= '`'.$Property['Name'].'` DOUBLE '.$Null.','; 190 215 else if($Property['Type'] == PropertyOneToMany) 191 $Query .= '`'.$Property['Name'].'` INT(255) NOT NULL,'.216 $Query .= '`'.$Property['Name'].'` INT(255)'.$Null.','. 192 217 'KEY `'.$Property['Name'].'` (`'.$Property['Name'].'`),'; 193 218 else if($Property['Type'] == PropertyManyToMany) ; … … 195 220 //$Query .= '`'.$Property['Name'].'` INT(255) NOT NULL,'. 196 221 // 'KEY `'.$Property['Name'].'` (`'.$Property['Name'].'`),'; 222 197 223 } 198 224 $Query .= 'PRIMARY KEY (`Id`)'. … … 202 228 { 203 229 if($Property['Type'] == PropertyOneToMany) 204 $this->Database->query('ALTER TABLE `'.$this->Name.'` ADD CONSTRAINT '. 205 '`'.$this->Name.'_ibfk_'.$Property['Name'].'` FOREIGN KEY (`'.$Property['TargetModel'].'`) REFERENCES `'.$Property['TargetModel'].'` (`Id`);'); 230 { 231 if(!$this->Module->Models[$Property['TargetModel']]->Installed) 232 $this->Module->Models[$Property['TargetModel']]->Install(); 233 $this->Database->query('ALTER TABLE `'.$this->Name.'` ADD FOREIGN KEY (`'.$Property['Name'].'`) REFERENCES `'.$Property['TargetModel'].'` (`Id`);'); 234 } 206 235 } 207 236 }
Note:
See TracChangeset
for help on using the changeset viewer.