public function update($id, array $record = null)
    {
        $model = is_a($id, $this->model_class) ? $id : null;
        if (null !== $model)
        {
            if ($record !== null)
                throw new DAOException("You should not pass an array of updates when supplying a Model");

            if ($id->getSourceDB() !== $this->db)
                throw new DAOException("Cannot update object - it did not originate in this database");

            $record = $id->getChanges();
            $id = $id->getID();
        }

        if ($record === null)
            throw new DAOException("No update provided");

        // Check if there's anything to update
        if (count($record) === 0)
            return 0;

        $table = $this->table;
        $columns = $table->getColumns();

        $pkey = $table->getPrimaryColumns();
        if ($pkey === null)
            throw new DAOException("Cannot update record without primary key");

        if (is_scalar($id) && count($pkey) === 1)
            foreach ($pkey as $colname => $def)
                $id = [$colname => $id];

        $condition = $this->getSelector($pkey, $id);

        // Remove primary key from the to-be-updated fields
        foreach ($pkey as $column)
            unset($record[$column->getName()]);

        $update = new Query\Update;
        $update->add(new Query\SourceTableClause($this->tablename));
        $update->add(new Query\WhereClause($condition));
        
        foreach ($record as $field => $value)
        {
            if (!isset($columns[$field]))
                throw new DAOException("Invalid field: " . $field);

            $coldef = $columns[$field];
            $value = $coldef->beforeInsertFilter($value);
            $update->add(new Query\UpdateField($field, $value));
        }

        $drv = $this->db->getDriver();
        $rows = $drv->update($update);

        if (null !== $model)
        {
            $model->markClean();
        }

        return $rows;
    }
public function update($id, array $record = null)
    {
        $model = is_a($id, $this->model_class) ? $id : null;
        if (null !== $model)
        {
            if ($record !== null)
                throw new DAOException("You should not pass an array of updates when supplying a Model");

            if ($id->getSourceDB() !== $this->db)
                throw new DAOException("Cannot update object - it did not originate in this database");

            $record = $id->getChanges();
            $id = $id->getID();
        }

        if ($record === null)
            throw new DAOException("No update provided");

        // Check if there's anything to update
        if (count($record) === 0)
            return 0;

        $table = $this->table;
        $columns = $table->getColumns();

        $pkey = $table->getPrimaryColumns();
        if ($pkey === null)
            throw new DAOException("Cannot update record without primary key");

        if (is_scalar($id) && count($pkey) === 1)
            foreach ($pkey as $colname => $def)
                $id = [$colname => $id];

        $condition = $this->getSelector($pkey, $id);

        // Remove primary key from the to-be-updated fields
        foreach ($pkey as $column)
            unset($record[$column->getName()]);

        $update = new Query\Update;
        $update->add(new Query\SourceTableClause($this->tablename));
        $update->add(new Query\WhereClause($condition));
        
        foreach ($record as $field => $value)
        {
            if (!isset($columns[$field]))
                throw new DAOException("Invalid field: " . $field);

            $coldef = $columns[$field];
            $value = $coldef->beforeInsertFilter($value);
            $update->add(new Query\UpdateField($field, $value));
        }

        $drv = $this->db->getDriver();
        $rows = $drv->update($update);

        if (null !== $model)
        {
            $model->markClean();
        }

        return $rows;
    }
public function update ( $ id , array $ record = null ) { $ model = is_a ( $ id , $ this -> model_class ) ? $ id : null ; if ( null !== $ model ) { if ( $ record !== null ) throw new DAOException ( "You should not pass an array of updates when supplying a Model" ) ; if ( $ id -> getSourceDB ( ) !== $ this -> db ) throw new DAOException ( "Cannot update object - it did not originate in this database" ) ; $ record = $ id -> getChanges ( ) ; $ id = $ id -> getID ( ) ; } if ( $ record === null ) throw new DAOException ( "No update provided" ) ; // Check if there's anything to update if ( count ( $ record ) === 0 ) return 0 ; $ table = $ this -> table ; $ columns = $ table -> getColumns ( ) ; $ pkey = $ table -> getPrimaryColumns ( ) ; if ( $ pkey === null ) throw new DAOException ( "Cannot update record without primary key" ) ; if ( is_scalar ( $ id ) && count ( $ pkey ) === 1 ) foreach ( $ pkey as $ colname => $ def ) $ id = [ $ colname => $ id ] ; $ condition = $ this -> getSelector ( $ pkey , $ id ) ; // Remove primary key from the to-be-updated fields foreach ( $ pkey as $ column ) unset ( $ record [ $ column -> getName ( ) ] ) ; $ update = new Query \ Update ; $ update -> add ( new Query \ SourceTableClause ( $ this -> tablename ) ) ; $ update -> add ( new Query \ WhereClause ( $ condition ) ) ; foreach ( $ record as $ field => $ value ) { if ( ! isset ( $ columns [ $ field ] ) ) throw new DAOException ( "Invalid field: " . $ field ) ; $ coldef = $ columns [ $ field ] ; $ value = $ coldef -> beforeInsertFilter ( $ value ) ; $ update -> add ( new Query \ UpdateField ( $ field , $ value ) ) ; } $ drv = $ this -> db -> getDriver ( ) ; $ rows = $ drv -> update ( $ update ) ; if ( null !== $ model ) { $ model -> markClean ( ) ; } return $ rows ; }
Update records in the database.

@param mixed $id The values for the primary key to select the record to update. You can also supply
an instance of the accompanying Model class.
@param array $record The record to update. Should contain key/value pairs where
keys are fieldnames, values are the values to update them to.
Should also contain the value for the primary key.
which will be used to find the record to be updated.
When providing a Model as $id, this should be omitted.
@return int The number of updated records
Update records in the database .