Скрипт
Для LS 0.3
<?php
$plugname = 'Test';
$entityname = 'Test';
$sql = 'CREATE TABLE IF NOT EXISTS `prefix_test` (
`test_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) unsigned NOT NULL,
`test_name` varchar(200) NOT NULL,
`test_type` int(3) NOT NULL,
`test_date` datetime NOT NULL,
';
$tab = ' ';
$br = "\n";
$aColumn = array();
$aEntityName = array();
$aRequest = array();
$aType = array();
preg_match('/`(.*?)`/', $sql, $match);
$DBname = $match[1];
$sql = strstr($sql, '(');
$count = preg_match_all('/`(.*?)`\s+(int)?/', $sql, $match);
for($y = 0; $y < $count; $y++) {
$colomn = $match[1][$y];
$aColumn[] = $colomn;
$aColomnToEntity = explode('_', $colomn);
$entity = null;
$i = (strtolower($entityname)==$aColomnToEntity[0]) ? 1 : 0;
for($c = count($aColomnToEntity); $i < $c; $i++ ) {
$entity .= ucfirst($aColomnToEntity[$i]);
}
$aEntityName[] = $entity;
$aRequest[] = end(explode('_', $colomn, 2));
$aType[] = $match[2][$y] ? '?d' : '?';
}
$sSubmit = 'submit_'.strtolower($entityname).'_add';
$entityClassName = $plugname.'Entity'.'_'.$entityname;
$objName = '$o'.$plugname.(($plugname != $entityname) ? $entityname : null);
echo '<pre>';
echo 'class '.$entityClassName.' extends Entity '.br();
echo '{'.br();
for($i = 0; $i < $count; $i++) {
echo tab().'public function get'.$aEntityName[$i].'() {'.br();
echo tab(2).'return $this->_aData[\''.$aColumn[$i].'\'];'.br();
echo tab().'}'.br();
}
echo br(2);
for($i = 0; $i < $count; $i++) {
echo tab().'public function set'.$aEntityName[$i].'($data) {'.br();
echo tab(2).'$this->_aData[\''.$aColumn[$i].'\']=$data;'.br();
echo tab().'}'.br();
}
echo '}'.br();
echo 'class Mapper_'.$plugname.' extends Mapper'.br();
echo '{'.br();
echo tab().'public function Add'.$entityname.'('.$entityClassName.' '.$objName.') {'.br();
echo tab(2).'$sql = \'INSERT INTO '.$DBname.' ('.br();
for($i = 1; $i < $count; $i++) {
echo tab(4).$aColumn[$i];
if($i != $count-1) echo ','; echo br();
}
echo tab(3).') VALUES ('.br();
echo tab(4).trim(strstr(implode(', ', $aType),' ')).br();
echo tab(3).')'.br();
echo tab(2).'\';'.br();
echo tab(2).'return $this->oDb->query($sql,'.br();
for($i = 1; $i < $count; $i++) {
echo tab(6).$objName.'->get'.$aEntityName[$i].'()';
if($i != $count-1) echo ','; echo br();
}
echo tab(2).');'.br();
echo tab().'}'.br();
echo tab().'public function Update'.$entityname.'('.$entityClassName.' '.$objName.') {'.br();
echo tab(2).'$sql = \'UPDATE '.$DBname.br();
echo tab(3).'SET'.br();
for($i = 1; $i < $count; $i++) {
echo tab(4).$aColumn[$i].' = '.$aType[$i];
if($i != $count-1) echo ','; echo br();
}
echo tab(3).'WHERE'.br();
echo tab(4).$aColumn[0].' = '.$aType[0].br();
echo tab(2).'\';'.br();
echo tab(2).'return $this->oDb->query($sql,'.br();
for($i = 1; $i < $count; $i++) {
echo tab(6).$objName.'->get'.$aEntityName[$i].'(),'.br();
}
echo tab(6).$objName.'->get'.$aEntityName[0].'()'.br();
echo tab(2).');'.br();
echo tab().'}'.br();
echo tab().'public function Get'.$entityname.'(&$iCount, $iPage, $iPerPage) {'.br();
echo tab(2).'$sql = \'SELECT'.br();
echo tab(4).'*'.br();
echo tab(3).'FROM'.br();
echo tab(4).$DBname.br();
echo tab(3).'WHERE'.br();
echo tab(4).'1=1'.br();
echo tab(2).'\';'.br();
echo tab(2).'$aResult = array();'.br();
echo tab(2).'if($aRows = $this->oDb->selectPage($iCount, $sql, ($iPage-1)*$iPerPage, $iPerPage)) {'.br();
echo tab(3).'foreach ($aRows as $aRow) {'.br();
echo tab(4).'$aResult[] = new '.$entityClassName.'($aRow);'.br();
echo tab(3).'}'.br();
echo tab(2).'}'.br();
echo tab(2).'return $aResult;'.br();
echo tab().'}'.br();
echo '}'.br();
echo 'class Ls'.$plugname.' extends Module {'.br(2);
echo tab().'protected $oMapper;'.br(2);
echo tab().'public function Init() {'.br();
echo tab(2).'$this->oMapper = new Mapper_'.$plugname.'($this->Database_GetConnect());'.br();
echo tab().'}'.br();
echo tab().'public function Add'.$entityname.'('.$entityClassName.' '.$objName.') {'.br();
echo tab(2).'return $this->oMapper->Add'.$entityname.'('.$objName.');'.br();
echo tab().'}'.br();
echo tab().'public function Update'.$entityname.'('.$entityClassName.' '.$objName.') {'.br();
echo tab(2).'return $this->oMapper->Update'.$entityname.'('.$objName.');'.br();
echo tab().'}'.br();
echo tab().'public function Get'.$plugname.'(&$iCount, $iPage, $iPerPage) {'.br();
echo tab(2).'return $this->oMapper->Get'.$entityname.'($iCount, $iPage, $iPerPage);'.br();
echo tab().'}'.br();
echo '}'.br();
echo tab().'protected function EventAdd() {'.br();
echo tab(2).'if(getRequest(\''.$sSubmit.'\')) {'.br();
echo tab(3).'if (!$this->check'.$entityname.'Fields()) {'.br();
echo tab(4).'return false;'.br();
echo tab(3).'}'.br();
echo tab(3).$objName.' = new '.$entityClassName.';'.br();
for($i = 1; $i < $count; $i++) {
$data = ereg('Date', $aEntityName[$i]) ? 'date("Y-m-d H:i:s")' : 'getRequest(\''.$aRequest[$i].'\')';
echo tab(3).$objName.'->set'.$aEntityName[$i].'('.$data.');'.br();
}
echo tab(3).'if($this->'.$plugname.'->Add'.$entityname.'('.$objName.')) {'.br();
echo tab(3).'}'.br();
echo tab(2).'}'.br();
echo tab().'}'.br();
echo '<textarea style="width:100%; height: 100%">';
echo tab().'<form method="POST">'.br();
for($i = 1; $i < $count; $i++) {
echo tab(2).'<p>'.br();
echo tab(3).'<label>:</label>'.br();
echo tab(3).'<input type="text" name="'.$aRequest[$i].'" value="{$_aRequest.'.$aRequest[$i].'}">
'.br();
echo tab(3).'<span class="form_note"></span>'.br();
echo tab(2).'</p>'.br();
}
echo tab(2).'<p>';
echo '<input type="submit" name="'.$sSubmit.'" value="Add">
';
echo '</p>'.br();
echo tab().'</form>'.br();
echo '</textarea>';
echo '</pre>';
function tab($c = 1) {
global $tab;
$ret_tab = null;
for($i = 0; $i < $c; $i++) {
$ret_tab .= $tab;
}
return $ret_tab;
}
function br($c = 1) {
global $br;
$ret_br = null;
for($i = 0; $i < $c; $i++) {
$ret_br .= $br;
}
return $ret_br;
}
?>
7 комментариев
Или вы это для себя опубликовали, чтоб не потерялось?