Postgres::addForeignKey PHP Method

addForeignKey() public method

Adds a foreign key constraint to a table
public addForeignKey ( $table, $targschema, $targtable, $sfields, $tfields, $upd_action, $del_action, $match, $deferrable, $initially, $name = '' ) : -1
$targschema The schema that houses the target table to which to add the foreign key
$targtable The table to which to add the foreign key
$sfields (array) An array of source fields over which to add the foreign key
$tfields (array) An array of target fields over which to add the foreign key
$upd_action The action for updates (eg. RESTRICT)
$del_action The action for deletes (eg. RESTRICT)
$match The match type (eg. MATCH FULL)
$deferrable The deferrability (eg. NOT DEFERRABLE)
$name (optional) The name to give the key, otherwise default name is assigned
return -1
    function addForeignKey($table, $targschema, $targtable, $sfields, $tfields, $upd_action, $del_action, $match, $deferrable, $initially, $name = '')
    {
        if (!is_array($sfields) || sizeof($sfields) == 0 || !is_array($tfields) || sizeof($tfields) == 0) {
            return -1;
        }
        $f_schema = $this->_schema;
        $this->fieldClean($f_schema);
        $this->fieldClean($table);
        $this->fieldClean($targschema);
        $this->fieldClean($targtable);
        $this->fieldArrayClean($sfields);
        $this->fieldArrayClean($tfields);
        $this->fieldClean($name);
        $sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" ADD ";
        if ($name != '') {
            $sql .= "CONSTRAINT \"{$name}\" ";
        }
        $sql .= "FOREIGN KEY (\"" . join('","', $sfields) . "\") ";
        // Target table needs to be fully qualified
        $sql .= "REFERENCES \"{$targschema}\".\"{$targtable}\"(\"" . join('","', $tfields) . "\") ";
        if ($match != $this->fkmatches[0]) {
            $sql .= " {$match}";
        }
        if ($upd_action != $this->fkactions[0]) {
            $sql .= " ON UPDATE {$upd_action}";
        }
        if ($del_action != $this->fkactions[0]) {
            $sql .= " ON DELETE {$del_action}";
        }
        if ($deferrable != $this->fkdeferrable[0]) {
            $sql .= " {$deferrable}";
        }
        if ($initially != $this->fkinitial[0]) {
            $sql .= " {$initially}";
        }
        return $this->execute($sql);
    }
Postgres