public function setNames($name) { $this->tblName = $name; $this->singularized = Singulars($this->tblName); $this->camelized = Camelize($this->singularized); return true; }
function MigrationsActions($params = NULL, &$obj = NULL) { if (isset($_POST['migrations'])) { $path = INST_PATH . 'migrations/'; $migrationsSelected = array(); foreach ($_POST['migrations'] as $field => $value) { if ($value === 'on') { $Obj = 'create_' . Singulars($field); $migrationsSelected[Camelize($Obj)] = $field; } elseif (preg_match('[:selected]', $field)) { $all = false; } } } // if(isset($all) and !$all and isset($migrationsSelected)): $var = $migrationsSelected; // endif; $action = NULL; if (isset($_POST['migrations']['reset:all']) or isset($_POST['migrations']['reset:selected'])) { $action = 'Reset'; } elseif (isset($_POST['migrations']['up:all']) or isset($_POST['migrations']['up:selected'])) { $action = 'up'; } elseif (isset($_POST['migrations']['down:all']) or isset($_POST['migrations']['down:selected'])) { $action = 'down'; } if (isset($var) and isset($action)) { foreach ($var as $Obj => $Tbl) { $file = $path . 'create_' . $Tbl . '.php'; require_once $file; $objaux = new $Obj(); $objaux->{$action}(); } } }
function create_migrationsAction(){ //Require_Admin_Login(); if(isset($_POST['fields']['sub'])): $arraux = array(); $arraux['Table'] = $_POST['create_migrations']['Table']; foreach($_POST['create_migrations'] as $row){ if($row != $_POST['create_migrations']['Table'] and (isset($_POST['create_migrations']['scaffold']) and $row != $_POST['create_migrations']['scaffold']) and $row['Field'] !== ''): $arraux[] = array('field' => $row['Field'], 'type' => $row['Type'], 'null' => (isset($row['NotNull']) and $row['NotNull'] == 'on')? 'false': 'true', 'limit' => (isset($row['Length']))? $row['Length']: NULL); endif; } $path=INST_PATH.'migrations/'; //$fp = fopen($path.'create_'.strtolower($arraux['Table']).'.php', "w+b"); $string = "<?php \n class Create".Camelize(Singulars(strtolower($arraux['Table']))); $string .= " extends Migrations{ \n"; $string .= "\tfunction up(){ \n"; $string .= "\t\t".'$this'."->Create_Table(array('Table'=>'".$arraux['Table']."', \n"; $i=2; foreach($arraux as $row){ if($row != $arraux['Table']): $string .= "\t\t\t\t\t\t\t\tarray('field'=>'".$row['field']."', 'type'=>'".$row['type']."', 'null'=>'".$row['null']."'"; if(isset($row['limit']) and $row['limit']) $string .= ", 'limit'=>'".$row['limit']."'"; $string .= ")"; if($i < sizeof($arraux)): $string .= ", \n"; else: $string .= "\n"; endif; $i++; endif; } $string .= "\t\t\t\t\t\t\t)); \n"; $string .= "\t}\n"; $string .= "\tfunction down(){\n"; $string .= "\t\t".'$this'."->Drop_Table('".$arraux['Table']."');\n"; $string .= "\t}\n}\n"; $string .= "?>"; file_put_contents($path.'create_'.strtolower($arraux['Table']).'.php', $string); //fwrite($fp, $string); //fclose($fp); if(isset($_POST['create_migrations']['scaffold']) and $_POST['create_migrations']['scaffold'] = 'on'): include_once('Scaffold.php'); endif; endif; header("Location: ".INST_URI."console/index"); exit; }
/** * Metodo publico WriteModel($table) * * Este metodo se encarga de escribir en disco el archivo preliminar de modelo. * @param string $table Nombre de la tabla para crearle el modelo. */ function WriteModel($table) { $file = Singulars($table) . ".php"; $modelpath = INST_PATH . 'app/models/'; if (!file_exists($modelpath . $file)) { $newModel = "<?php \n"; $newModel .= "class " . Camelize(str_replace('.php', '', $file)) . " extends ActiveRecord {\n"; $newModel .= "\tfunction __construct(){ \n"; $newModel .= "\t} \n"; $newModel .= "} \n"; $newModel .= "?>"; $fp = fopen($modelpath . $file, "w+b"); fputs($fp, $newModel); fclose($fp); } }
$content .= "\t\t\t".'<? foreach($this->data as $row){ ?>'."\n"; $content .= "\t\t\t<tr>\n"; $content .= "\t\t\t".'<? foreach($row->getArray() as $col){ ?>'."\n"; $content .= "\t\t\t\t".'<td><?=$col;?></td>'."\n"; $content .= "\t\t\t<? } ?>\n"; $content .= "\t\t\t\t<td><a href=\"<?=INST_URI;?>$file/delete/<?=\$row->id;?>\">delete</a> <a href=\"<?=INST_URI;?>$file/addedit/<?=\$row->id;?>\">Edit</a></td>\n"; $content .= "\t\t\t</tr>\n"; $content .= "\t\t\t<? } ?>\n"; $content .= "\t\t</table>\n"; $content .= "\t</div>\n"; $content .= "<a href=\"<?=INST_URI;?>$file/addedit/\">Add new...</a>\n</div>"; fwrite($fp, $content); fclose($fp); //Creation of add/edit template $name = Singulars($arraux['Table']); $fileModel = $name.".php"; include_once(INST_PATH.'app/models/'.$fileModel); $objModel = new $ClassToUse(); $fields = $objModel->GetFields(); $fp = fopen(INST_PATH.'app/templates/'.$file.'/addedit.phtml', 'w+b'); $content = '<div style="float:left; width:100%;">'."\n"; $content .= "\t".'<div align="center">'."\n"; $content .= "\t\t<form action=\"<?=INST_URI;?>{$file}/create/\" method=\"post\" name=\"$name\" id=\"{$ClassToUse}_id\">\n"; foreach($fields as $field => $type){ $content .= "\t\t\t<label>$field:\n"; $input = $this->GetInput($type);
/** * método mágico __call() * * Este método es ejecutado por PHP cuando se accede a un m?todo que no existe. * A través de este método mágico se realiza el mapeo relacional, asumiendo que el llamado * a un método, es el llamado a un modelo realiza la subconsulta según las condiciones de relación y devuelve el * objeto del modelo requerido. * Es imperativo que las variables {@link $has_many}, {@link $belongs_to}, {@link $has_one}, {@link $has_and_belongs_to}, * estén correctamente creadas con las tablas a las que tienen su relación. * Tambien define el comportamiento cuando se hace un llamado a un método no definido en la clase, invocando a la función * definida en las extensiones. * Incluso define el comportamiento del método Find_by_{campo_en_la_tabla}(). * * @param string $ClassName Nombre del método al que se invoca. * @param mixed $val Valor pasado por parámetro dentro del método. */ public function __call($ClassName, $val = NULL) { $field = Singulars(strtolower($ClassName)); $classFromCall = Camelize($ClassName); if (preg_match('/Find_by_/', $ClassName)) { $nustring = str_replace("Find_by_", '', $ClassName); return $this->Find(array('conditions' => '`' . $nustring . "`='" . $val[0] . "'")); } elseif (file_exists(INST_PATH . 'app/models/' . $field . '.php')) { $way = isset($val[0]) ? $val[0] : 'down'; $foreign = strtolower($field) . "_id"; $prefix = unCamelize(get_class($this)); if (!class_exists($classFromCall)) { require_once INST_PATH . 'app/models/' . $field . '.php'; } $obj1 = new $classFromCall(); $conditions = "`" . $prefix . "_id`='" . $this->id . "'"; if (get_parent_class($obj1) == 'ActiveRecord') { if ($classFromCall == get_class($this) and in_array($ClassName, $this->has_many_and_belongs_to)) { $conditions = $way == 'up' ? "id='" . $this->{$foreign} . "'" : $conditions; } elseif (in_array($ClassName, $this->belongs_to)) { $conditions = "id='" . $this->{$foreign} . "'"; } return $conditions !== NULL ? $obj1->Find(array('conditions' => $conditions)) : $obj1->Niu(); } return NULL; } else { return $ClassName($val, $this); } }