SwatDB/SwatDB.php
public static function updateBinding(
        $db,
        $table,
        $id_field,
        $id,
        $value_field,
        $values,
        $bound_table,
        $bound_field
    ) {
        $id_field = new SwatDBField($id_field, 'integer');
        $value_field = new SwatDBField($value_field, 'integer');
        $bound_field = new SwatDBField($bound_field, 'integer');

        $values = self::initArray($values);

        $delete_sql = 'delete from %s where %s = %s';

        $delete_sql = sprintf(
            $delete_sql,
            $table,
            $id_field->name,
            $db->quote($id, $id_field->type)
        );

        if (count($values)) {
            foreach ($values as &$value) {
                $value = $db->quote($value, $value_field->type);
            }

            $value_list = implode(',', $values);

            $insert_sql =
                'insert into %s (%s, %s) select %s, %s from %s ' .
                'where %s not in (select %s from %s where %s = %s) and %s in (%s)';

            $insert_sql = sprintf(
                $insert_sql,
                $table,
                $id_field->name,
                $value_field->name,
                $db->quote($id, $id_field->type),
                $bound_field->name,
                $bound_table,
                $bound_field->name,
                $value_field->name,
                $table,
                $id_field->name,
                $db->quote($id, $id_field->type),
                $bound_field->name,
                $value_list
            );

            $delete_sql .= sprintf(
                ' and %s not in (%s)',
                $value_field->name,
                $value_list
            );
        }

        $transaction = new SwatDBTransaction($db);
        try {
            if (count($values)) {
                self::exec($db, $insert_sql);
            }

            self::exec($db, $delete_sql);
        } catch (Exception $e) {
            $transaction->rollback();
            throw $e;
        }
        $transaction->commit();
    }
public static function updateBinding(
        $db,
        $table,
        $id_field,
        $id,
        $value_field,
        $values,
        $bound_table,
        $bound_field
    ) {
        $id_field = new SwatDBField($id_field, 'integer');
        $value_field = new SwatDBField($value_field, 'integer');
        $bound_field = new SwatDBField($bound_field, 'integer');

        $values = self::initArray($values);

        $delete_sql = 'delete from %s where %s = %s';

        $delete_sql = sprintf(
            $delete_sql,
            $table,
            $id_field->name,
            $db->quote($id, $id_field->type)
        );

        if (count($values)) {
            foreach ($values as &$value) {
                $value = $db->quote($value, $value_field->type);
            }

            $value_list = implode(',', $values);

            $insert_sql =
                'insert into %s (%s, %s) select %s, %s from %s ' .
                'where %s not in (select %s from %s where %s = %s) and %s in (%s)';

            $insert_sql = sprintf(
                $insert_sql,
                $table,
                $id_field->name,
                $value_field->name,
                $db->quote($id, $id_field->type),
                $bound_field->name,
                $bound_table,
                $bound_field->name,
                $value_field->name,
                $table,
                $id_field->name,
                $db->quote($id, $id_field->type),
                $bound_field->name,
                $value_list
            );

            $delete_sql .= sprintf(
                ' and %s not in (%s)',
                $value_field->name,
                $value_list
            );
        }

        $transaction = new SwatDBTransaction($db);
        try {
            if (count($values)) {
                self::exec($db, $insert_sql);
            }

            self::exec($db, $delete_sql);
        } catch (Exception $e) {
            $transaction->rollback();
            throw $e;
        }
        $transaction->commit();
    }
public static function updateBinding ( $ db , $ table , $ id_field , $ id , $ value_field , $ values , $ bound_table , $ bound_field ) { $ id_field = new SwatDBField ( $ id_field , 'integer' ) ; $ value_field = new SwatDBField ( $ value_field , 'integer' ) ; $ bound_field = new SwatDBField ( $ bound_field , 'integer' ) ; $ values = self :: initArray ( $ values ) ; $ delete_sql = 'delete from %s where %s = %s' ; $ delete_sql = sprintf ( $ delete_sql , $ table , $ id_field -> name , $ db -> quote ( $ id , $ id_field -> type ) ) ; if ( count ( $ values ) ) { foreach ( $ values as & $ value ) { $ value = $ db -> quote ( $ value , $ value_field -> type ) ; } $ value_list = implode ( ',' , $ values ) ; $ insert_sql = 'insert into %s (%s, %s) select %s, %s from %s ' . 'where %s not in (select %s from %s where %s = %s) and %s in (%s)' ; $ insert_sql = sprintf ( $ insert_sql , $ table , $ id_field -> name , $ value_field -> name , $ db -> quote ( $ id , $ id_field -> type ) , $ bound_field -> name , $ bound_table , $ bound_field -> name , $ value_field -> name , $ table , $ id_field -> name , $ db -> quote ( $ id , $ id_field -> type ) , $ bound_field -> name , $ value_list ) ; $ delete_sql .= sprintf ( ' and %s not in (%s)' , $ value_field -> name , $ value_list ) ; } $ transaction = new SwatDBTransaction ( $ db ) ; try { if ( count ( $ values ) ) { self :: exec ( $ db , $ insert_sql ) ; } self :: exec ( $ db , $ delete_sql ) ; } catch ( Exception $ e ) { $ transaction -> rollback ( ) ; throw $ e ; } $ transaction -> commit ( ) ; }
Update a binding table

Convenience method to update rows in a binding table. It will delete
and insert rows as necessary.

@param MDB2_Driver_Common $db The database connection.

@param string $table The binding table to update.

@param string $id_field The name of the binding table field that contains
the fixed value.  Can be given in the form type:name where type is
a standard MDB2 datatype. If type is ommitted, then integer is
assummed for this field.

@param mixed $id The value to store in the $id_field. The type should
correspond to the type of $id_field.

@param string $value_field The name of the binding table field that
contains the values from the bound table.  Can be given in the
form type:name where type is a standard MDB2 datatype. If type is
ommitted, then integer is assummed for this field.

@param array $values An array of values that should be stored in the
$value_field. The type of the individual values should
correspond to the type of $value_field.

@param string $bound_table The table bound through the binding table.

@param string $bound_field The database field in the bound table that the
binding table references.

@throws SwatDBException
Update a binding table