| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
require_once 'propel/engine/builder/om/php5/PHP5ComplexPeerBuilder.php'; |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class SfPeerBuilder extends PHP5ComplexPeerBuilder |
|---|
| 20 |
{ |
|---|
| 21 |
public function build() |
|---|
| 22 |
{ |
|---|
| 23 |
$peerCode = parent::build(); |
|---|
| 24 |
if (!DataModelBuilder::getBuildProperty('builderAddComments')) |
|---|
| 25 |
{ |
|---|
| 26 |
$peerCode = sfToolkit::stripComments($peerCode); |
|---|
| 27 |
} |
|---|
| 28 |
if (!DataModelBuilder::getBuildProperty('builderAddIncludes')) |
|---|
| 29 |
{ |
|---|
| 30 |
|
|---|
| 31 |
$peerCode = preg_replace("/(include|require)_once\s*.*MapBuilder\.php.*\s*/", "", $peerCode); |
|---|
| 32 |
} |
|---|
| 33 |
return $peerCode; |
|---|
| 34 |
} |
|---|
| 35 |
|
|---|
| 36 |
protected function addIncludes(&$script) |
|---|
| 37 |
{ |
|---|
| 38 |
if (!DataModelBuilder::getBuildProperty('builderAddIncludes')) |
|---|
| 39 |
{ |
|---|
| 40 |
return; |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
parent::addIncludes($script); |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
protected function addSelectMethods(&$script) |
|---|
| 47 |
{ |
|---|
| 48 |
parent::addSelectMethods($script); |
|---|
| 49 |
|
|---|
| 50 |
if ($this->getTable()->getAttribute('isI18N')) |
|---|
| 51 |
{ |
|---|
| 52 |
$this->addDoSelectWithI18n($script); |
|---|
| 53 |
$this->addI18nMethods($script); |
|---|
| 54 |
} |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
protected function addI18nMethods(&$script) |
|---|
| 58 |
{ |
|---|
| 59 |
$table = $this->getTable(); |
|---|
| 60 |
foreach ($table->getReferrers() as $fk) |
|---|
| 61 |
{ |
|---|
| 62 |
$tblFK = $fk->getTable(); |
|---|
| 63 |
if ($tblFK->getName() == $table->getAttribute('i18nTable')) |
|---|
| 64 |
{ |
|---|
| 65 |
$i18nClassName = $tblFK->getPhpName(); |
|---|
| 66 |
break; |
|---|
| 67 |
} |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
$script .= " |
|---|
| 71 |
|
|---|
| 72 |
/** |
|---|
| 73 |
* Returns the i18n model class name. |
|---|
| 74 |
* |
|---|
| 75 |
* @return string The i18n model class name |
|---|
| 76 |
*/ |
|---|
| 77 |
public static function getI18nModel() |
|---|
| 78 |
{ |
|---|
| 79 |
return '$i18nClassName'; |
|---|
| 80 |
} |
|---|
| 81 |
"; |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
protected function addDoSelectWithI18n(&$script) |
|---|
| 85 |
{ |
|---|
| 86 |
$table = $this->getTable(); |
|---|
| 87 |
$thisTableObjectBuilder = OMBuilder::getNewObjectBuilder($table); |
|---|
| 88 |
$className = $table->getPhpName(); |
|---|
| 89 |
$pks = $table->getPrimaryKey(); |
|---|
| 90 |
$pk = PeerBuilder::getColumnName($pks[0], $className); |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
foreach ($table->getReferrers() as $fk) |
|---|
| 94 |
{ |
|---|
| 95 |
$tblFK = $fk->getTable(); |
|---|
| 96 |
if ($tblFK->getName() == $table->getAttribute('i18nTable')) |
|---|
| 97 |
{ |
|---|
| 98 |
$i18nClassName = $tblFK->getPhpName(); |
|---|
| 99 |
|
|---|
| 100 |
$i18nPeerClassName = $i18nClassName.'Peer'; |
|---|
| 101 |
|
|---|
| 102 |
$i18nTable = $table->getDatabase()->getTable($tblFK->getName()); |
|---|
| 103 |
$i18nTableObjectBuilder = OMBuilder::getNewObjectBuilder($i18nTable); |
|---|
| 104 |
$i18nTablePeerBuilder = OMBuilder::getNewPeerBuilder($i18nTable); |
|---|
| 105 |
$i18nPks = $i18nTable->getPrimaryKey(); |
|---|
| 106 |
$i18nPk = PeerBuilder::getColumnName($i18nPks[0], $i18nClassName); |
|---|
| 107 |
|
|---|
| 108 |
$culturePhpName = ''; |
|---|
| 109 |
$cultureColumnName = ''; |
|---|
| 110 |
foreach ($tblFK->getColumns() as $col) |
|---|
| 111 |
{ |
|---|
| 112 |
if (("true" === strtolower($col->getAttribute('isCulture')))) |
|---|
| 113 |
{ |
|---|
| 114 |
$culturePhpName = $col->getPhpName(); |
|---|
| 115 |
$cultureColumnName = PeerBuilder::getColumnName($col, $i18nClassName); |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|
| 118 |
} |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
$script .= " |
|---|
| 122 |
|
|---|
| 123 |
/** |
|---|
| 124 |
* Selects a collection of $className objects pre-filled with their i18n objects. |
|---|
| 125 |
* |
|---|
| 126 |
* @return array Array of $className objects. |
|---|
| 127 |
* @throws PropelException Any exceptions caught during processing will be |
|---|
| 128 |
* rethrown wrapped into a PropelException. |
|---|
| 129 |
*/ |
|---|
| 130 |
public static function doSelectWithI18n(Criteria \$c, \$culture = null, \$con = null) |
|---|
| 131 |
{ |
|---|
| 132 |
if (\$culture === null) |
|---|
| 133 |
{ |
|---|
| 134 |
\$culture = sfPropel::getDefaultCulture(); |
|---|
| 135 |
} |
|---|
| 136 |
"; |
|---|
| 137 |
|
|---|
| 138 |
if (DataModelBuilder::getBuildProperty('builderAddBehaviors')) |
|---|
| 139 |
{ |
|---|
| 140 |
$script .= " |
|---|
| 141 |
|
|---|
| 142 |
foreach (sfMixer::getCallables('{$this->getClassname()}:doSelectJoin:doSelectJoin') as \$callable) |
|---|
| 143 |
{ |
|---|
| 144 |
call_user_func(\$callable, '{$this->getClassname()}', \$c, \$con); |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
"; |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|
| 150 |
$script .= " |
|---|
| 151 |
// Set the correct dbName if it has not been overridden |
|---|
| 152 |
if (\$c->getDbName() == Propel::getDefaultDB()) |
|---|
| 153 |
{ |
|---|
| 154 |
\$c->setDbName(self::DATABASE_NAME); |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
".$this->getPeerClassname()."::addSelectColumns(\$c); |
|---|
| 158 |
\$startcol = (".$this->getPeerClassname()."::NUM_COLUMNS - ".$this->getPeerClassname()."::NUM_LAZY_LOAD_COLUMNS) + 1; |
|---|
| 159 |
|
|---|
| 160 |
".$i18nPeerClassName."::addSelectColumns(\$c); |
|---|
| 161 |
|
|---|
| 162 |
\$c->addJoin(".$pk.", ".$i18nPk."); |
|---|
| 163 |
\$c->add(".$cultureColumnName.", \$culture); |
|---|
| 164 |
|
|---|
| 165 |
\$rs = ".$this->basePeerClassname."::doSelect(\$c, \$con); |
|---|
| 166 |
\$results = array(); |
|---|
| 167 |
|
|---|
| 168 |
while(\$rs->next()) { |
|---|
| 169 |
"; |
|---|
| 170 |
if ($table->getChildrenColumn()) { |
|---|
| 171 |
$script .= " |
|---|
| 172 |
\$omClass = ".$this->getPeerClassname()."::getOMClass(\$rs, 1); |
|---|
| 173 |
"; |
|---|
| 174 |
} else { |
|---|
| 175 |
$script .= " |
|---|
| 176 |
\$omClass = ".$this->getPeerClassname()."::getOMClass(); |
|---|
| 177 |
"; |
|---|
| 178 |
} |
|---|
| 179 |
$script .= " |
|---|
| 180 |
\$cls = Propel::import(\$omClass); |
|---|
| 181 |
\$obj1 = new \$cls(); |
|---|
| 182 |
\$obj1->hydrate(\$rs); |
|---|
| 183 |
\$obj1->setCulture(\$culture); |
|---|
| 184 |
"; |
|---|
| 185 |
|
|---|
| 186 |
$script .= " |
|---|
| 187 |
.$i18nTablePeerBuilder->getPeerClassname()."::getOMClass(\$rs, \$startcol); |
|---|
| 188 |
; |
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
$script .= " |
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
.$i18nClassName."ForCulture(\$obj2, \$culture); |
|---|
| 201 |
.$className."(\$obj1); |
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
; |
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
addDoValidate(&$script) |
|---|
| 211 |
|
|---|
| 212 |
$tmp = ''; |
|---|
| 213 |
parent::addDoValidate($tmp); |
|---|
| 214 |
|
|---|
| 215 |
$script .= str_replace("return {$this->basePeerClassname}::doValidate(".$this->getPeerClassname()."::DATABASE_NAME, ".$this->getPeerClassname()."::TABLE_NAME, \$columns);\n", |
|---|
| 216 |
"\$res = {$this->basePeerClassname}::doValidate(".$this->getPeerClassname()."::DATABASE_NAME, ".$this->getPeerClassname()."::TABLE_NAME, \$columns);\n". |
|---|
| 217 |
" if (\$res !== true) {\n". |
|---|
| 218 |
" \$request = sfContext::getInstance()->getRequest();\n". |
|---|
| 219 |
" foreach (\$res as \$failed) {\n". |
|---|
| 220 |
" \$col = ".$this->getPeerClassname()."::translateFieldname(\$failed->getColumn(), BasePeer::TYPE_COLNAME, BasePeer::TYPE_PHPNAME);\n". |
|---|
| 221 |
" \$request->setError(\$col, \$failed->getMessage());\n". |
|---|
| 222 |
" }\n". |
|---|
| 223 |
" }\n\n". |
|---|
| 224 |
" return \$res;\n", $tmp); |
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
addDoSelectRS(&$script) |
|---|
| 228 |
|
|---|
| 229 |
$tmp = ''; |
|---|
| 230 |
parent::addDoSelectRS($tmp); |
|---|
| 231 |
|
|---|
| 232 |
DataModelBuilder::getBuildProperty('builderAddBehaviors')) |
|---|
| 233 |
|
|---|
| 234 |
$mixer_script = " |
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
; |
|---|
| 242 |
$tmp = preg_replace('/public static function doSelect(RS|Join.*)\(Criteria \$(c|criteria), \$con = null\)\n\s*{/', '\0'.$mixer_script, $tmp); |
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
$script .= $tmp; |
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
addDoSelectJoin(&$script) |
|---|
| 249 |
|
|---|
| 250 |
$tmp = ''; |
|---|
| 251 |
parent::addDoSelectJoin($tmp); |
|---|
| 252 |
|
|---|
| 253 |
DataModelBuilder::getBuildProperty('builderAddBehaviors')) |
|---|
| 254 |
|
|---|
| 255 |
$mixer_script = " |
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
; |
|---|
| 263 |
$tmp = preg_replace('/public static function doSelectJoin.*\(Criteria \$c, \$con = null\)\n\s*{/', '\0'.$mixer_script, $tmp); |
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
$script .= $tmp; |
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
addDoSelectJoinAll(&$script) |
|---|
| 270 |
|
|---|
| 271 |
$tmp = ''; |
|---|
| 272 |
parent::addDoSelectJoinAll($tmp); |
|---|
| 273 |
|
|---|
| 274 |
DataModelBuilder::getBuildProperty('builderAddBehaviors')) |
|---|
| 275 |
|
|---|
| 276 |
$mixer_script = " |
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
; |
|---|
| 284 |
$tmp = preg_replace('/public static function doSelectJoinAll\(Criteria \$c, \$con = null\)\n\s*{/', '\0'.$mixer_script, $tmp); |
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
$script .= $tmp; |
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 |
addDoSelectJoinAllExcept(&$script) |
|---|
| 291 |
|
|---|
| 292 |
$tmp = ''; |
|---|
| 293 |
parent::addDoSelectJoinAllExcept($tmp); |
|---|
| 294 |
|
|---|
| 295 |
DataModelBuilder::getBuildProperty('builderAddBehaviors')) |
|---|
| 296 |
|
|---|
| 297 |
$mixer_script = " |
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
; |
|---|
| 305 |
$tmp = preg_replace('/public static function doSelectJoinAllExcept.*\(Criteria \$c, \$con = null\)\n\s*{/', '\0'.$mixer_script, $tmp); |
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
$script .= $tmp; |
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
addDoUpdate(&$script) |
|---|
| 312 |
|
|---|
| 313 |
$tmp = ''; |
|---|
| 314 |
parent::addDoUpdate($tmp); |
|---|
| 315 |
|
|---|
| 316 |
DataModelBuilder::getBuildProperty('builderAddBehaviors')) |
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 |
$pre_mixer_script = " |
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 |
|
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
; |
|---|
| 331 |
|
|---|
| 332 |
$post_mixer_script = " |
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 |
|
|---|
| 340 |
; |
|---|
| 341 |
|
|---|
| 342 |
$tmp = preg_replace('/{/', '{'.$pre_mixer_script, $tmp, 1); |
|---|
| 343 |
$tmp = preg_replace("/\t\treturn ([^}]+)/", "\t\t\$ret = $1".$post_mixer_script.' ', $tmp, 1); |
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 |
$script .= $tmp; |
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
addDoInsert(&$script) |
|---|
| 350 |
|
|---|
| 351 |
$tmp = ''; |
|---|
| 352 |
parent::addDoInsert($tmp); |
|---|
| 353 |
|
|---|
| 354 |
DataModelBuilder::getBuildProperty('builderAddBehaviors')) |
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
$pre_mixer_script = " |
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
; |
|---|
| 369 |
|
|---|
| 370 |
$post_mixer_script = " |
|---|
| 371 |
|
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 |
; |
|---|
| 377 |
|
|---|
| 378 |
$tmp = preg_replace('/{/', '{'.$pre_mixer_script, $tmp, 1); |
|---|
| 379 |
$tmp = preg_replace("/\t\treturn/", "\t\t".$post_mixer_script, $tmp, 1); |
|---|
| 380 |
|
|---|
| 381 |
|
|---|
| 382 |
$script .= $tmp; |
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
addClassClose(&$script) |
|---|
| 386 |
|
|---|
| 387 |
parent::addClassClose($script); |
|---|
| 388 |
|
|---|
| 389 |
$behavior_file_name = 'Base'.$this->getTable()->getPhpName().'Behaviors'; |
|---|
| 390 |
$behavior_file_path = $this->getFilePath($this->getStubObjectBuilder()->getPackage().'.om.'.$behavior_file_name); |
|---|
| 391 |
$absolute_behavior_file_path = sfConfig::get('sf_root_dir').'/'.$behavior_file_path; |
|---|
| 392 |
|
|---|
| 393 |
file_exists($absolute_behavior_file_path)) |
|---|
| 394 |
|
|---|
| 395 |
unlink($absolute_behavior_file_path); |
|---|
| 396 |
|
|---|
| 397 |
|
|---|
| 398 |
$behaviors = $this->getTable()->getAttribute('behaviors'); |
|---|
| 399 |
$behaviors) |
|---|
| 400 |
|
|---|
| 401 |
file_put_contents($absolute_behavior_file_path, sprintf("<?php\nsfPropelBehavior::add('%s', %s);\n", $this->getTable()->getPhpName(), var_export(unserialize($behaviors), true))); |
|---|
| 402 |
$script .= sprintf("\n\ninclude_once '%s';\n", $behavior_file_path); |
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
|
|---|