Horde_Rdo_Mapper::update PHP Method

update() public method

Updates a record in the backend. $object can be either a primary key or an Rdo object. If $object is an Rdo instance then $fields will be ignored as values will be pulled from the object.
public update ( string | Rdo $object, array $fields = null ) : integer
$object string | Rdo The Rdo instance or unique id to update.
$fields array If passing a unique id, the array of field properties to set for $object.
return integer Number of objects updated.
    public function update($object, $fields = null)
    {
        if ($object instanceof Horde_Rdo_Base) {
            $key = $this->primaryKey;
            $id = $object->{$key};
            $fields = iterator_to_array($object);
            if (!$id) {
                // Object doesn't exist yet; create it instead.
                $o = $this->create($fields);
                $this->mapFields($object, iterator_to_array($o));
                return 1;
            }
        } else {
            $id = $object;
        }
        // If configured to record update time, set it here.
        if ($this->_setTimestamps) {
            $fields['updated_at'] = time();
        }
        // Filter out any extra fields.
        $fields = array_intersect_key($fields, array_flip($this->tableDefinition->getColumnNames()));
        if (!$fields) {
            // Nothing to change.
            return 0;
        }
        $sql = 'UPDATE ' . $this->adapter->quoteTableName($this->table) . ' SET';
        $bindParams = array();
        foreach ($fields as $field => $value) {
            $sql .= ' ' . $this->adapter->quoteColumnName($field) . ' = ?,';
            $bindParams[] = $value;
        }
        $sql = substr($sql, 0, -1) . ' WHERE ' . $this->primaryKey . ' = ?';
        $bindParams[] = $id;
        return $this->adapter->update($sql, $bindParams);
    }