Spot\Adapter\PDO\BaseAbstract::update PHP Метод

update() публичный Метод

Update entity
public update ( $datasource, array $data, array $where = [], array $options = [] )
$data array
$where array
$options array
    public function update($datasource, array $data, array $where = array(), array $options = array())
    {
        $dataBinds = $this->statementBinds($data, 0);
        $whereBinds = $this->statementBinds($where, count($dataBinds));
        $binds = array_merge($dataBinds, $whereBinds);
        $placeholders = array();
        $dataFields = array_combine(array_keys($data), array_keys($dataBinds));
        // Placeholders and passed data
        foreach ($dataFields as $field => $bindField) {
            $placeholders[] = $this->escapeField($field) . " = :" . $bindField . "";
        }
        $conditions = $this->statementConditions($where, count($dataBinds));
        // Ensure there are actually updated values on THIS table
        if (count($binds) > 0) {
            // Build the query
            $sql = "UPDATE " . $datasource . " SET " . implode(', ', $placeholders) . " WHERE " . $conditions;
            // Add query to log
            \Spot\Log::addQuery($this, $sql, $binds);
            try {
                // Prepare update query
                $stmt = $this->connection()->prepare($sql);
                if ($stmt) {
                    // Execute
                    if ($stmt->execute($binds)) {
                        $result = true;
                    } else {
                        $result = false;
                    }
                } else {
                    $result = false;
                }
            } catch (\PDOException $e) {
                // Table does not exist
                if ($e->getCode() == "42S02") {
                    throw new \Spot\Exception_Datasource_Missing("Table or datasource '" . $datasource . "' does not exist");
                }
                // Re-throw exception
                throw $e;
            }
        } else {
            $result = false;
        }
        return $result;
    }