Exemplo n.º 1
0
 private function DBInsertSingleTable($sTableClass)
 {
     $sTable = MetaModel::DBGetTable($sTableClass);
     // Abstract classes or classes having no specific attribute do not have an associated table
     if ($sTable == '') {
         return;
     }
     $sClass = get_class($this);
     // fields in first array, values in the second
     $aFieldsToWrite = array();
     $aValuesToWrite = array();
     if (!empty($this->m_iKey) && $this->m_iKey >= 0) {
         // Add it to the list of fields to write
         $aFieldsToWrite[] = '`' . MetaModel::DBGetKey($sTableClass) . '`';
         $aValuesToWrite[] = CMDBSource::Quote($this->m_iKey);
     }
     $aHierarchicalKeys = array();
     foreach (MetaModel::ListAttributeDefs($sTableClass) as $sAttCode => $oAttDef) {
         // Skip this attribute if not defined in this table
         if (!MetaModel::IsAttributeOrigin($sTableClass, $sAttCode)) {
             continue;
         }
         $aAttColumns = $oAttDef->GetSQLValues($this->m_aCurrValues[$sAttCode]);
         foreach ($aAttColumns as $sColumn => $sValue) {
             $aFieldsToWrite[] = "`{$sColumn}`";
             $aValuesToWrite[] = CMDBSource::Quote($sValue);
         }
         if ($oAttDef->IsHierarchicalKey()) {
             $aHierarchicalKeys[$sAttCode] = $oAttDef;
         }
     }
     if (count($aValuesToWrite) == 0) {
         return false;
     }
     if (MetaModel::DBIsReadOnly()) {
         $iNewKey = -1;
     } else {
         if (self::$m_bBulkInsert) {
             if (!isset(self::$m_aBulkInsertCols[$sClass][$sTable])) {
                 self::$m_aBulkInsertCols[$sClass][$sTable] = implode(', ', $aFieldsToWrite);
             }
             self::$m_aBulkInsertItems[$sClass][$sTable][] = '(' . implode(', ', $aValuesToWrite) . ')';
             $iNewKey = 999999;
             // TODO - compute next id....
         } else {
             if (count($aHierarchicalKeys) > 0) {
                 foreach ($aHierarchicalKeys as $sAttCode => $oAttDef) {
                     $aValues = MetaModel::HKInsertChildUnder($this->m_aCurrValues[$sAttCode], $oAttDef, $sTable);
                     $aFieldsToWrite[] = '`' . $oAttDef->GetSQLRight() . '`';
                     $aValuesToWrite[] = $aValues[$oAttDef->GetSQLRight()];
                     $aFieldsToWrite[] = '`' . $oAttDef->GetSQLLeft() . '`';
                     $aValuesToWrite[] = $aValues[$oAttDef->GetSQLLeft()];
                 }
             }
             $sInsertSQL = "INSERT INTO `{$sTable}` (" . join(",", $aFieldsToWrite) . ") VALUES (" . join(", ", $aValuesToWrite) . ")";
             $iNewKey = CMDBSource::InsertInto($sInsertSQL);
         }
     }
     // Note that it is possible to have a key defined here, and the autoincrement expected, this is acceptable in a non root class
     if (empty($this->m_iKey)) {
         // Take the autonumber
         $this->m_iKey = $iNewKey;
     }
     return $this->m_iKey;
 }