示例#1
0
 private function _relationshipProperties()
 {
     $relation = phpClassGenerator::$relatedField;
     $nb = count($relation);
     for ($a = 0; $a < $nb; $a++) {
         $relationFound = false;
         # 1:N relation
         if (array_key_exists('relationType', phpClassGenerator::$relatedField[$a]) && phpClassGenerator::$relatedField[$a]['relationType'] == '1:n') {
             //check if the relation regards current object
             if ($relation[$a]['object'] == $this->getName()) {
                 //search related object:
                 //in relation we have src table, field and object
                 //now we searching in all other object->table the primary field matching the src.table.field
                 $objectList = phpClassGenerator::$objects;
                 $nb2 = count($objectList);
                 //Zend_Debug::Dump($relation[$a]);
                 for ($b = 0; $b < $nb2; $b++) {
                     //we remowe the current object from search area because the self relationship are not yet supported
                     if ($objectList[$b]['object']->getName() != $this->getName()) {
                         foreach ($objectList[$b]['object']->properties as $propertyName => $infos) {
                             //if object->properties->infos->fieldname == relation->fieldname &&
                             // object->properties->infos->fieldname->primary == true
                             if ($infos['fieldName'] == $relation[$a]['toField'] && $infos['primary']) {
                                 //THIS FIELD MATCH !!!
                                 //								Zend_Debug::Dump($infos);
                                 //								Zend_Debug::Dump($objectList[$b]['object']->getName());
                                 $relatedObjectName = $objectList[$b]['object']->getName();
                                 phpClassGenerator::$relatedField[$a]['relatedObject'] = $relatedObjectName;
                                 phpClassGenerator::$relatedField[$a]['relatedPropertyName'] = $propertyName;
                                 phpClassGenerator::$relatedField[$a]['relationType'] = '1:n';
                                 $relationFound = true;
                                 break;
                             }
                         }
                         if ($relationFound) {
                             break;
                         }
                     }
                     if ($relationFound) {
                         break;
                     }
                 }
             }
             if ($relationFound) {
                 $this->_append('/**');
                 $this->_append(' * relationship with ' . $relatedObjectName);
                 $this->_append(' * @var ' . $relatedObjectName);
                 $this->_append(' */');
                 $this->_append('private $_' . $relatedObjectName . ';');
             }
         }
         # 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->getName()) {
                 //now match if the field is the foreign key
                 if (preg_match('#^' . $srcTable . '#', $relation[$a]['toField'])) {
                     $linkedObjectName = $srcObject->getName();
                     phpClassGenerator::$relatedField[$a]['srcObject'] = $srcObject->getName();
                     phpClassGenerator::$relatedField[$a]['relatedObject'] = $linkedObject->getName();
                     phpClassGenerator::$relatedField[$a]['relatedPropertyName'] = null;
                     phpClassGenerator::$relatedField[$a]['relationType'] = '1:1';
                 } else {
                     $linkedObjectName = $linkedObject->getName();
                     $propertyName = '_' . phpClassGenerator::formatPropertyName($relation[$a]['toField']);
                     $this->_append('/**');
                     $this->_append(' * relationship with ' . $linkedObjectName);
                     $this->_append(' * @var ' . $linkedObjectName);
                     $this->_append(' */');
                     $this->_append('private $' . $propertyName . ';');
                     phpClassGenerator::$relatedField[$a]['srcObject'] = $srcObject->getName();
                     phpClassGenerator::$relatedField[$a]['relatedObject'] = $linkedObjectName;
                     phpClassGenerator::$relatedField[$a]['relatedPropertyName'] = $propertyName;
                     phpClassGenerator::$relatedField[$a]['relationType'] = '1:1';
                 }
             }
         }
         # N:M RELATION
         if (array_key_exists('relationType', phpClassGenerator::$relatedField[$a]) && phpClassGenerator::$relatedField[$a]['relationType'] == 'n:m') {
             //in this mode all column are foreign. we must build a collection of linked object
             $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 SRC object else do nothing
             if ($srcObject->getName() == $this->getName()) {
                 if (preg_match('#^' . $srcTable . '#', phpClassGenerator::$relatedField[$a]['toField'])) {
                     $linkedObjectName = $linkedObject->getName();
                     $propertyName = '_' . $linkedObjectName . '_collection';
                     phpClassGenerator::$relatedField[$a]['srcObject'] = $srcObject->getName();
                     phpClassGenerator::$relatedField[$a]['relatedObject'] = $linkedObject->getName();
                     phpClassGenerator::$relatedField[$a]['relatedPropertyName'] = $propertyName;
                     phpClassGenerator::$relatedField[$a]['relationType'] = 'n:m';
                     $this->_append('/**');
                     $this->_append(' * relationship with ' . $linkedObjectName);
                     $this->_append(' * @var ' . $linkedObjectName . '_collection');
                     $this->_append(' */');
                     $this->_append('private $' . $propertyName . ';');
                     //						Zend_Debug::Dump($a);
                     //						Zend_Debug::Dump(phpClassGenerator::$relatedField[$a]);
                 }
             }
             if ($linkedObject->getName() == $this->getName()) {
                 if (preg_match('#^' . $linkedTable . '#', phpClassGenerator::$relatedField[$a]['toField'])) {
                     $linkedObjectName = $srcObject->getName();
                     $propertyName = '_' . $linkedObjectName . '_collection';
                     phpClassGenerator::$relatedField[$a]['srcObject'] = $linkedObject->getName();
                     phpClassGenerator::$relatedField[$a]['relatedObject'] = $srcObject->getName();
                     phpClassGenerator::$relatedField[$a]['relatedPropertyName'] = $propertyName;
                     phpClassGenerator::$relatedField[$a]['relationType'] = 'n:m';
                     $this->_append('/**');
                     $this->_append(' * relationship with ' . $linkedObjectName);
                     $this->_append(' * @var ' . $linkedObjectName . '_collection');
                     $this->_append(' */');
                     $this->_append('private $' . $propertyName . ';');
                     //						Zend_Debug::Dump($a);
                     //						Zend_Debug::Dump(phpClassGenerator::$relatedField[$a]);
                 }
             }
         }
     }
 }
示例#2
0
 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('}');
 }