예제 #1
0
파일: Base.php 프로젝트: railsphp/railsphp
 private function _save_do()
 {
     $w = $wd = $q = $d = array();
     $dt = static::table()->columnNames();
     $indexes = static::table()->indexes() ?: [];
     foreach (array_keys($this->changedAttributes()) as $attrName) {
         # Can't update properties that don't have a column in DB, or
         # PRImary keys, or time columns.
         if (!in_array($attrName, $dt) || $attrName == 'created_at' || $attrName == 'updated_at' || $attrName == 'created_on' || $attrName == 'updated_on' || in_array($attrName, $indexes)) {
             continue;
         } else {
             $q[] = '`' . $attrName . '` = ?';
             $d[] = $this->getAttribute($attrName);
         }
     }
     if (!$q) {
         # Nothing to update
         return true;
     }
     if ($indexes) {
         foreach ($indexes as $idx) {
             $w[] = '`' . $idx . '` = ?';
             if ($this->attributeChanged($idx)) {
                 $wd[] = $this->attributeWas($idx);
             } else {
                 $wd[] = $this->getAttribute($idx);
             }
         }
     } else {
         foreach ($this->attributes() as $attrName => $value) {
             $w[] = '`' . $attrName . '` = ?';
             if ($this->attributeChanged($attrName)) {
                 $wd[] = $this->attributeWas($attrName);
             } elseif (($value = $this->getAttribute($attrName)) !== null) {
                 $wd[] = $value;
             }
         }
     }
     # Update `updated_at|on` if exists.
     if ($this->_check_time_column('updated_at')) {
         $q[] = "`updated_at` = ?";
         $d[] = $this->updated_at;
     } elseif ($this->_check_time_column('updated_on')) {
         $q[] = "`updated_on` = ?";
         $d[] = $this->updated_on;
     }
     if ($q) {
         $q = "UPDATE `" . static::tableName() . "` SET " . implode(', ', $q);
         $w && ($q .= ' WHERE ' . implode(' AND ', $w));
         $q .= ' LIMIT 1';
         $d = array_merge($d, $wd);
         array_unshift($d, $q);
         static::connection()->executeSql($d);
         if (ActiveRecord::lastError()) {
             # The error is logged by Connection.
             return false;
         }
     }
     $this->_update_init_attrs();
     return true;
 }
예제 #2
0
 public function selectValue()
 {
     $stmt = call_user_func_array([$this, 'executeSql'], func_get_args());
     if (ActiveRecord::lastError()) {
         return false;
     }
     if ($data = $stmt->fetch()) {
         $data = array_shift($data);
     }
     return $data;
 }