/** * Update the entity in the database. The entity * knows how to update it based on the primary key. * * @param DBEntity $obj the object to update. */ public function update(&$obj) { $versionColumn = null; $columns = $obj->getColumns(); if (!$obj->isPersistent()) { trigger_error("Object {$obj} is not set as persistent!", E_USER_NOTICE); $obj->setDAO($this); } // Create the UPDATE clause $set = []; $where = []; $data = get_object_vars($obj); foreach ($columns as $name => $definition) { $val = @$data[$name]; $columnName = $definition->getName(); $sqlVal = $this->converter->sqlOf($definition, $val); if ($definition->isPrimaryKey()) { //if (strlen($where) > 0) $where .= " AND "; $where[] = "{$columnName} = {$sqlVal}"; } else { if ($definition->isVersion()) { // if (strlen($where) > 0) $where .= " AND "; $where[] = "{$columnName} = {$sqlVal}"; // if( strlen($set) > 0 ) $set .= ", "; $set[] = "{$columnName} = {$columnName} + 1"; $versionColumn = $name; } else { if (!$definition->isAutomatic() && !is_null($sqlVal)) { //if( strlen($set) > 0 ) $set .= ", "; $set[] = "{$columnName} = {$sqlVal}"; } } } } $table = $obj->getTableName(); $sql = "UPDATE {$table} SET " . implode(", ", $set) . " WHERE " . implode(" AND ", $where); $nb = $this->execute($sql); if ($nb > 1) { // $log = KLogger::getDefault(); $errs = $dao->errorInfo(); $message .= "\nSQL: {$sql}\nError code: {$errs['0']}\nError message: {$errs['2']}"; $this->dao->sqlError($sql, $errs[2]); if ((error_reporting() && E_ERROR) == E_ERROR) { echo "<pre>{$message}</pre>"; } die(1); } else { if ($nb == 1) { if ($versionColumn) { // Update the version (done during the UPDATE) // reflect on the current. $this->{$versionColumn}++; } } } return $nb < 2; // If an update DOES NOT UPDATE anything (no changes in columns, it can return 0). }