Пример #1
0
 /**
  * Generate and execute an insert SQL statement for the given object.
  *
  * @param array   &$object  The object we wish to insert.
  * @param string  $table    The treated table reference.
  * @param string  $idfield  The column which stores the primary key (optional) (default='id').
  * @param boolean $preserve Whether or not to preserve existing/set standard fields (optional) (default=false).
  * @param boolean $force    Whether or not to insert empty values as NULL (optional) (default=false).
  *
  * @return The result set from the update operation. The object is updated with the newly generated ID.
  * @deprecated
  * @see    Doctrine_Record::save()
  * @deprecated
  * @see    Doctrine_Table
  * @throws Exception If column or column_def is not an array or cant find anything to insert into object.
  */
 public static function insertObject(array &$object, $table, $idfield = 'id', $preserve = false, $force = false)
 {
     $tables = self::getTables();
     $tableName = $tables[$table];
     $sql = "INSERT INTO {$tableName} ";
     // set standard architecture fields
     ObjectUtil::setStandardFieldsOnObjectCreate($object, $preserve, $idfield);
     // build the column list
     $columnList = $tables["{$table}_column"];
     if (!is_array($columnList)) {
         throw new Exception(__f('%s_column is not an array', $table));
     }
     // build the column definition list
     $columnDefList = $tables["{$table}_column_def"];
     if (!is_array($columnDefList)) {
         throw new Exception(__f('%s_column_def is not an array', $table));
     }
     // grab each key and value and append to the sql string
     $search = array('+', '-', '*', '/', '%');
     $replace = array('');
     $cArray = array();
     $vArray = array();
     $dbDriverName = strtolower(Doctrine_Manager::getInstance()->getCurrentConnection()->getDriverName());
     foreach ($columnList as $key => $val) {
         $hasMath = (bool) strcmp($val, str_replace($search, $replace, $val));
         if ($hasMath) {
             continue;
         }
         if (isset($object[$key])) {
             $skip = false;
             $save = false;
             $columnDefinition = $columnDefList[$key];
             $columnDefFields = explode(' ', $columnDefinition);
             $colType = substr($columnDefinition, 0, 1);
             // ensure that international float numbers are stored with '.' rather than ',' for decimal separator
             if ($colType == 'F' || $colType == 'N') {
                 if (is_float($object[$key]) || is_double($object[$key])) {
                     $object[$key] = number_format($object[$key], 8, '.', '');
                 }
             }
             // generate the actual insert values
             if (!$skip) {
                 $cArray[] = $columnList[$key];
                 $value = is_bool($object[$key]) ? (int) $object[$key] : $object[$key];
                 if (($dbDriverName == 'derby' || $dbDriverName == 'splice' || $dbDriverName == 'jdbcbridge') && (strtoupper($columnDefFields[0]) != 'XL' || strtoupper($columnDefFields[0]) != 'B') && strlen($object[$key]) > 32000) {
                     $chunks = str_split($object[$key], 32000);
                     $str = '';
                     foreach ($chunks as $chunk) {
                         if ($str) {
                             $str .= ' || ';
                         }
                         $str = "CAST (" . self::_formatForStore($chunk) . " AS CLOB)";
                     }
                     $vArray[] = self::_formatForStore($str);
                 } else {
                     $vArray[] = self::_typesafeQuotedValue($table, $key, $object[$key]);
                 }
             }
         } else {
             if ($key == $idfield) {
                 if ($dbDriverName == 'pgsql') {
                     $cArray[] = $columnList[$key];
                     $vArray[] = 'DEFAULT';
                 }
             } elseif ($force) {
                 $cArray[] = $columnList[$key];
                 $vArray[] = 'NULL';
             }
         }
     }
     if (!($cArray && $vArray)) {
         throw new Exception(__('Unable to find anything to insert in supplied object ...'));
     }
     $sql .= ' (' . implode(',', $cArray) . ')';
     $sql .= ' VALUES (' . implode(',', $vArray) . ')';
     $res = self::executeSQL($sql);
     if ($res === false) {
         return $res;
     }
     self::flushCache($table);
     if (!isset($object[$idfield]) || !$object[$idfield] || (!$preserve || !isset($object[$idfield])) && isset($columnList[$idfield])) {
         if (isset($columnDefList[$idfield])) {
             $columnDefinition = $columnDefList[$idfield];
             $columnDefFields = explode(' ', $columnDefinition);
             $colType = substr($columnDefinition, 0, 1);
             $colAuto = in_array('AUTO', $columnDefFields);
             if ($colType == 'I' && $colAuto) {
                 $obj_id = self::getInsertID($table, $idfield);
                 $object[$idfield] = $obj_id;
             }
         }
     }
     if ($cArray && $vArray) {
         $object = self::_savePostProcess($object, $table, $idfield);
     }
     return $object;
 }