/** * list all database table's * * @return array */ static function listTable() { $results = self::$db->fetchAll("SHOW TABLES"); $nb = count($results); for ($a = 0; $a < $nb; $a++) { $_tables[]['name'] = $results[$a][key($results[$a])]; } self::$tables = $_tables; return $_tables; }
private function _relationshipGetter() { $this->_append('/**'); $this->_append('* magic get method'); $this->_append('* used to construct relationship between objects'); $this->_append('*'); $this->_append('* @param string $name'); $this->_append('*/'); $this->_append('public function __get($name){'); $relation = phpClassGenerator::$relatedField; $nb = count($relation); for ($a = 0; $a < $nb; $a++) { if ($relation[$a]['relationType'] == '1:n') { if ($relation[$a]['object'] == $this->getName()) { $relatedObjectName = $relation[$a]['relatedObject']; $primaryFieldName = $relation[$a]['toField']; $primaryVarName = phpClassGenerator::formatPropertyName($primaryFieldName); $primaryGetterName = phpClassGenerator::formatPropertyName('get_' . $primaryFieldName); $relatedPropertyGetterName = phpClassGenerator::formatPropertyName('get_' . $relation[$a]['relatedPropertyName']); $this->_append('if($name == \'' . $relatedObjectName . '\'){'); $this->_append('if(!$this->_' . $relatedObjectName . '){'); $this->_append('#1, n mode'); $this->_append('$' . $primaryVarName . ' = $this->' . $primaryGetterName . '();'); $this->_append('$this->_' . $relatedObjectName . ' = new ' . $relatedObjectName . '($' . $primaryVarName . ');'); $this->_append('}'); $this->_append('elseif(is_object($this->_' . $relatedObjectName . ')){'); $this->_append('if($this->_' . $relatedObjectName . '->' . $relatedPropertyGetterName . '() != $this->' . $primaryGetterName . '()){'); $this->_append('$' . $primaryVarName . ' = $this->' . $primaryGetterName . '();'); $this->_append('$this->_' . $relatedObjectName . ' = new ' . $relatedObjectName . '($' . $primaryVarName . ');'); $this->_append('}'); $this->_append('}'); $this->_append('return $this->_' . $relatedObjectName . ';'); $this->_append('}'); } } elseif ($relation[$a]['relationType'] == '1:1') { if ($relation[$a]['srcObject'] == $this->getName()) { //check for real property if ($relation[$a]['relatedPropertyName']) { $relatedObjectName = $relation[$a]['relatedObject']; $propertyName = $relation[$a]['relatedPropertyName']; // _property $calledPropertyName = substr($relation[$a]['relatedPropertyName'], 1); // property $relatedObject = phpClassGenerator::getObjectByName($relatedObjectName); $relatedPropertyGetterName = phpClassGenerator::formatPropertyName('get_' . $relatedObject->getPrimaryKeyName()); $this->_append('if($name == \'' . $calledPropertyName . '\'){'); $this->_append('if(!$this->' . $propertyName . '){'); $this->_append('#1:1 mode'); $this->_append('$' . $calledPropertyName . ' = $this->' . $calledPropertyName . ';'); $this->_append('$this->' . $propertyName . ' = new ' . $relatedObjectName . '($' . $calledPropertyName . ');'); $this->_append('}'); $this->_append('elseif(is_object($this->' . $propertyName . ')){'); $this->_append('if($this->' . $propertyName . '->' . $relatedPropertyGetterName . '() != $this->' . $calledPropertyName . '()){'); $this->_append('$' . $calledPropertyName . ' = $this->' . $calledPropertyName . ';'); $this->_append('$this->' . $propertyName . ' = new ' . $relatedObjectName . '($' . $calledPropertyName . ');'); $this->_append('}'); $this->_append('}'); $this->_append('return $this->' . $propertyName . ';'); $this->_append('}'); } } } elseif ($relation[$a]['relationType'] == 'n:m') { if ($relation[$a]['srcObject'] == $this->getName()) { //check for real property if ($relation[$a]['relatedPropertyName']) { $relatedObjectName = $relation[$a]['relatedObject']; $propertyName = $relation[$a]['relatedPropertyName']; // _property $calledPropertyName = substr($relation[$a]['relatedPropertyName'], 1); // property $relatedObject = phpClassGenerator::getObjectByName($relatedObjectName); $relatedPropertyGetterName = phpClassGenerator::formatPropertyName('get_' . $relatedObject->getPrimaryKeyName()); $this->_append('if($name == \'' . $calledPropertyName . '\' || $name == \'' . $relatedObjectName . 's\'){'); $this->_append('if(!$this->' . $propertyName . '){'); $this->_append('#n:m mode'); $this->_append('$' . $relatedObject->getPrimaryKeyName() . ' = $this->' . $relatedPropertyGetterName . '();'); $this->_append('$this->' . $propertyName . ' = new ' . $calledPropertyName . '();'); $this->_append('$this->' . $propertyName . '->select("SELECT * FROM ' . $relation[$a]['fromTable'] . ' WHERE ' . $relation[$a]['srcObject'] . '_' . $relatedObject->getPrimaryKeyName() . ' = ".$' . $relatedObject->getPrimaryKeyName() . ');'); $this->_append('}'); $this->_append('return $this->' . $propertyName . ';'); $this->_append('}'); } } } } $this->_append('throw new Exception("Try to access to an unknown property");'); $this->_append('}'); }
<?php /** * This file is a part of php class generator (PCG) apps. * * licence: Cecill http://www.cecill.info/licences/Licence_CeCILL_V1.1-US.html * author: Cyril Janssens * $Id$ */ #ini_set('include_path', '.;C:\opt\lib'); ini_set('include_path', '.;./lib'); function __autoload($className) { if (is_file('lib/class.' . $className . '.php')) { require_once 'lib/class.' . $className . '.php'; } elseif (is_file('out/class.' . $className . '.php')) { require_once 'out/class.' . $className . '.php'; } elseif ($className == 'Zend_Loader') { require_once 'Zend/Loader.php'; } else { Zend_Loader::loadClass($className); } } phpClassGenerator::$userZendLoader = true; phpClassGenerator::factory(); phpClassGenerator::listTable(); phpClassGenerator::makeAllObjects(); phpClassGenerator::makeAll();
private function _save() { $this->_append('/**'); $this->_append(' * ' . $this->baseName . ' saver.'); $this->_append(' * '); $this->_append(' * @return ' . $this->baseName); $this->_append(' */'); $this->_append('public static function save(' . $this->baseName . ' $' . $this->baseName . '=null){'); $this->_append('self::factory();'); $this->_append('if(!$' . $this->baseName . '){'); $this->_append('$' . $this->baseName . ' = self::$' . $this->baseName . ';'); $this->_append('}'); $fields = $this->object->getProperties(); //$primary = $this->primary; $getPrimaryKeyFunction = $this->primaryGetter; $setPrimaryKeyFunction = $this->primarySetter; #CASE UPDATE ROW $this->_append('if($' . $this->baseName . '->' . $getPrimaryKeyFunction . '()){'); //add relation in save fonction $relation = phpClassGenerator::$relatedField; $nb = count($relation); $related11tables = array(); $relatedNMtables = array(); for ($a = 0; $a < $nb; $a++) { # 1:1 relation if (array_key_exists('relationType', phpClassGenerator::$relatedField[$a]) && phpClassGenerator::$relatedField[$a]['relationType'] == '1:1') { //in this mode the're one direct column linked and all other int column are object of linked table (srctable)_has_(linkedtable) $matches = array(); preg_match("#(.+)_has_(.+)#", $relation[$a]['fromTable'], $matches); $srcTable = $matches[1]; $linkedTable = $matches[2]; //search which objects match with table name foreach (phpClassGenerator::$objects as $objects) { if ($objects['object']->getTableName() == $srcTable) { $srcObject = $objects['object']; } if ($objects['object']->getTableName() == $linkedTable) { $linkedObject = $objects['object']; } } //check if we are in scr object else do nothing if ($srcObject->getName() == $this->object->getName()) { //now match if the field is the foreign key //if(preg_match('#^'.$srcTable.'#',$relation[$a]['toField'])){ // add table name in key, for unique naming $related11tables[$relation[$a]['fromTable']][] = $relation[$a]; //} } } # N:M RELATION if (array_key_exists('relationType', phpClassGenerator::$relatedField[$a]) && phpClassGenerator::$relatedField[$a]['relationType'] == 'n:m') { //in this mode we must empty the lines in the relation table and refill with the new collection value $matches = array(); preg_match("#(.+)_has_(.+)#", $relation[$a]['fromTable'], $matches); $srcTable = $matches[1]; $linkedTable = $matches[2]; //search which objects match with table name $srcObject = phpClassGenerator::getObjectByTableName($srcTable); $linkedObject = phpClassGenerator::getObjectByTableName($linkedTable); //check if we are in scr object else do nothing if ($srcObject->getName() == $this->object->getName()) { //unifying request if (!array_key_exists($srcObject->getName() . ' ' . $this->object->getName(), $relatedNMtables)) { $primarygetterName = phpClassGenerator::formatPropertyName('get_' . $srcObject->getPrimaryKeyName()); $primaryLinkedGetterName = phpClassGenerator::formatPropertyName('get_' . $linkedObject->getPrimaryKeyName()); $SQLemptyingTable = 'DELETE FROM ' . $relation[$a]['fromTable'] . ' WHERE ' . $srcObject->getTableName() . '_' . $srcObject->getPrimaryKeyName() . ' = \'.$' . $this->baseName . '->' . $primarygetterName . '()'; $this->_append('self::$db->query(\'' . $SQLemptyingTable . ');'); $relatedNMtables[$srcObject->getName() . ' ' . $this->object->getName()] = true; $NMinsert[] = $this->_append('$i=0;'); $NMinsert[] = $this->_append('$insert = \'INSERT INTO ' . $relation[$a]['fromTable'] . ' (' . $srcObject->getTableName() . '_' . $srcObject->getPrimaryKeyName() . ', ' . $linkedObject->getTableName() . '_' . $linkedObject->getPrimaryKeyName() . ') VALUES \';'); $NMinsert[] = $this->_append('$collection = self::$' . $this->baseName . '->' . $linkedObject->getName() . '_collection;'); $NMinsert[] = $this->_append('foreach ($collection as $' . $linkedObject->getName() . '){'); $NMinsert[] = $this->_append('$insert .= $i !== 0 ? "," : "";'); $NMinsert[] = $this->_append('$insert .= \'(\'.$' . $this->baseName . '->' . $primarygetterName . '().\',\'.$' . $linkedObject->getName() . '->' . $primaryLinkedGetterName . '().\')\';'); $NMinsert[] = $this->_append('$i++;'); $NMinsert[] = $this->_append('}'); $NMinsert[] = $this->_append('if($i!==0){'); $NMinsert[] = $this->_append('self::$db->query($insert);'); $NMinsert[] = $this->_append('}'); } } } } $this->_append('$update = "UPDATE ' . $this->tableName . ' '); foreach ($related11tables as $tableName => $foreignFields) { $this->_append(',' . $tableName); } $this->_append(' SET ";'); $this->_append('$_update = array();'); $i = 0; foreach ($fields as $propertyName => $params) { if (!$params['primary']) { $this->_append('if($' . $this->baseName . '->getModifier(\'' . $propertyName . '\')){'); $this->_append('$_update[] = "' . $params['fieldName'] . ' = \'".$' . $this->baseName . '->' . phpClassGenerator::formatPropertyName('get_' . $propertyName) . '()."\'";'); $this->_append('}'); $i++; } else { $primaryKeyField = $params['fieldName']; } } $this->_append('if(count($_update) > 0){'); $this->_append('for($a=0; $a < count($_update);$a++){'); $this->_append('$update .= ($a === 0 ? "" : ",").$_update[$a];'); $this->_append('}'); $this->_append('$update .= " WHERE ' . $this->tableName . '.' . $primaryKeyField . ' = ".$' . $this->baseName . '->' . $getPrimaryKeyFunction . '();'); foreach ($related11tables as $tableName => $foreignFields) { $this->_append('$update .= " AND ' . $tableName . '.' . $primaryKeyField . ' = ".$' . $this->baseName . '->' . $getPrimaryKeyFunction . '();'); } $this->_append('self::$db->query($update);'); $this->_append('}'); $this->_append('}'); #CASE NEW ROW $listFields = ''; $listFieldsValue = ''; $this->_append('else{'); $i = 0; foreach ($fields as $propertyName => $params) { if (!$params['primary'] && !$params['foreignField']) { $listFields .= ($i === 0 ? '' : ',') . $params['fieldName']; $listFieldsValue .= $i === 0 ? '' : ','; $listFieldsValue .= "'\".\$" . $this->baseName . "->" . phpClassGenerator::formatPropertyName('get_' . $propertyName) . "().\"'"; $i++; } } $this->_append('self::$db->query("INSERT INTO ' . $this->tableName . ' ('); $this->_append($listFields); $this->_append(') VALUES ('); $this->_append($listFieldsValue); $this->_append(')");'); $this->_append('self::$' . $this->baseName . '->' . $setPrimaryKeyFunction . '(self::$db->lastInsertId());'); //now INSERT for 1:1 relationship $i = 0; foreach ($related11tables as $tableName => $foreignFields) { if ($i === 0) { $listForeignFields = ''; $listForeignFieldsValue = ''; } $this->_append('self::$db->query("INSERT INTO ' . $tableName . ' ('); $nb = count($foreignFields); for ($a = 0; $a < $nb; $a++) { $listForeignFields .= ($a === 0 ? '' : ',') . $foreignFields[$a]['toField']; if ($foreignFields[$a]['relatedPropertyName']) { $listForeignFieldsValue .= ($a === 0 ? '' : ',') . "'\".\$" . $this->baseName . "->" . phpClassGenerator::formatPropertyName('get_' . $foreignFields[$a]['toField']) . "().\"'"; } else { $listForeignFieldsValue .= ($a === 0 ? '' : ',') . "'\".\$" . $this->baseName . "->" . $getPrimaryKeyFunction . "().\"'"; } } $this->_append($listForeignFields . ') VALUES (' . $listForeignFieldsValue . ')'); $this->_append('");'); $i++; if (count($tableName) == $i) { $i = 0; } } //N:M insert $nb = count($NMinsert); for ($a = 0; $a < $nb; $a++) { $this->_append($NMinsert[$a]); } $this->_append('}'); $this->_append('}'); }
private function _addRemoveCount() { $this->_append('/**'); $this->_append(' * add ' . $this->baseName . ' to collection'); $this->_append(' *'); $this->_append(' * @param ' . $this->baseName . ' $' . $this->baseName); $this->_append(' * @param [integer] $mode @see ' . $this->name . '::select'); $this->_append(' * @return ' . $this->name); $this->_append(' */ '); $this->_append('public function add(' . $this->baseName . ' $' . $this->baseName . ', $mode=1){'); $this->_append('$existFlag = false;'); $this->_append('//set context object'); $this->_append('$' . $this->baseName . '->setContextObject($this);'); $innerVar = phpClassGenerator::formatPropertyName('$inner_' . $this->baseName); $this->_append('foreach ($this as $key => ' . $innerVar . '){'); $this->_append('//check for existing ' . $this->baseName); $this->_append('if(' . $innerVar . '->getId() == $' . $this->baseName . '->getId()){'); $this->_append('$existFlag = true;'); $this->_append('$existKey = $key;'); $this->_append('break;'); $this->_append('}'); $this->_append('}'); $this->_append('if($existFlag && $mode == self::MODE_APPEND){'); $this->_append('// do nothing'); $this->_append('}'); $this->_append('else if($existFlag && $mode == self::MODE_REPLACE){'); $this->_append('//replace'); $this->_append('$this->_set($existKey, $' . $this->baseName . ');'); $this->_append('}'); $this->_append('else if(!$existFlag && ($mode == self::MODE_APPEND || $mode == self::MODE_APPEND)){'); $this->_append('//simply add'); $this->_append('$this->_add($' . $this->baseName . ');'); $this->_append('}'); $this->_append('else{'); $this->_append('//what ??!'); $this->_append('return false;'); $this->_append('}'); $this->_append('return $this;'); $this->_append('}'); $this->_append('/**'); $this->_append('* count collection elements'); $this->_append('*'); $this->_append('* @param [mixed] $count by ref'); $this->_append('* @return ' . $this->name . '|integer'); $this->_append('*/'); $this->_append('public function count(&$count=null){'); $this->_append('if(!is_null($count)){'); $this->_append('$count = count($this->items);'); $this->_append('return $this;'); $this->_append('}'); $this->_append('else{'); $this->_append('return count($this->items); '); $this->_append('}'); $this->_append('}'); $this->_append('/**'); $this->_append(' * remove current item from collection'); $this->_append(' *'); $this->_append(' * @param ' . $this->baseName . ' $' . $this->baseName . ''); $this->_append(' * @return boolean'); $this->_append(' */'); $this->_append('public function remove(' . $this->baseName . ' $' . $this->baseName . '){'); $this->_append('$existFlag = false;'); $this->_append('foreach ($this as $key => ' . $innerVar . '){'); $this->_append('//check for existing bugs'); $this->_append('if(' . $innerVar . '->getId() == $' . $this->baseName . '->getId()){'); $this->_append('$existFlag = true;'); $this->_append('$existKey = $key;'); $this->_append('break;'); $this->_append('}'); $this->_append('}'); $this->_append('if($existFlag){'); $this->_append('unset($this->items[$existKey]);'); $this->_append('return true;'); $this->_append('}'); $this->_append('else{'); $this->_append('return false;'); $this->_append('}'); $this->_append('}'); }