Пример #1
0
 /**
  * Inserts a new row.
  *
  * @param  array  $data  Column-value pairs.
  * @return mixed         The primary key of the row inserted.
  */
 public static function insert(array $data)
 {
     /**
      * Zend_Db_Table assumes that if you have a compound primary key
      * and one of the columns in the key uses a sequence,
      * it's the _first_ column in the compound key.
      */
     $pkIdentity = static::$_primary[static::$_identity];
     /**
      * If this table uses a database sequence object and the data does not
      * specify a value, then get the next ID from the sequence and add it
      * to the row.  We assume that only the first column in a compound
      * primary key takes a value from a sequence.
      */
     if (is_string(static::$_sequence) && !isset($data[$pkIdentity])) {
         $data[$pkIdentity] = static::$_db->nextSequenceId(static::$_sequence);
         $pkSuppliedBySequence = true;
     }
     /**
      * If the primary key can be generated automatically, and no value was
      * specified in the user-supplied data, then omit it from the tuple.
      * 
      * Note: this checks for sensible values in the supplied primary key
      * position of the data.  The following values are considered empty:
      *   null, false, true, '', array()
      */
     if (!isset($pkSuppliedBySequence) && array_key_exists($pkIdentity, $data)) {
         if ($data[$pkIdentity] === null || $data[$pkIdentity] === '' || is_bool($data[$pkIdentity]) || is_array($data[$pkIdentity]) && empty($data[$pkIdentity])) {
             // empty array
             unset($data[$pkIdentity]);
         }
     }
     /**
      * INSERT the new row.
      */
     $tableSpec = (static::$_schema ? static::$_schema . '.' : '') . static::$_name;
     static::$_db->insert($tableSpec, $data);
     /**
      * Fetch the most recent ID generated by an auto-increment
      * or IDENTITY column, unless the user has specified a value,
      * overriding the auto-increment mechanism.
      */
     if (static::$_sequence === true && !isset($data[$pkIdentity])) {
         $data[$pkIdentity] = static::$_db->lastInsertId();
     }
     /**
      * Return the primary key value if the PK is a single column,
      * else return an associative array of the PK column/value pairs.
      */
     $pkData = array_intersect_key($data, array_flip(static::$_primary));
     if (count(static::$_primary) == 1) {
         reset($pkData);
         return current($pkData);
     }
     return $pkData;
 }
Пример #2
0
 /**
  * Inserts Ignore a new row.
  *
  * @param  array  $data  Column-value pairs.
  * @return \mysqli_result mysql result
  */
 public function insertIgnore(array $data)
 {
     return $this->_db->insert('IGNORE')->into(($this->_schema ? $this->_schema . '.' : '') . $this->_name)->columns(array_keys($data))->values(array_values($data))->query();
 }