samples/PhotoAlbum/lib/rb.php
public function addFK( $type, $targetType, $field, $targetField, $isDep = false) {
		try{
			$table = $this->safeTable($type);
			$column = $this->safeColumn($field);
			$tableNoQ = $this->safeTable($type,true);
			$columnNoQ = $this->safeColumn($field,true);
			$targetTable = $this->safeTable($targetType);
			$targetTableNoQ = $this->safeTable($targetType,true);
			$targetColumn  = $this->safeColumn($targetField);
			$targetColumnNoQ  = $this->safeColumn($targetField,true);
			
			
			$sql = "SELECT
					tc.constraint_name, 
					tc.table_name, 
					kcu.column_name, 
					ccu.table_name AS foreign_table_name,
					ccu.column_name AS foreign_column_name,
					rc.delete_rule
					FROM 
					information_schema.table_constraints AS tc 
					JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
					JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
					JOIN information_schema.referential_constraints AS rc ON ccu.constraint_name = rc.constraint_name
					WHERE constraint_type = 'FOREIGN KEY' AND tc.table_catalog=current_database()
					AND tc.table_name = '$tableNoQ' 
					AND ccu.table_name = '$targetTableNoQ'
					AND kcu.column_name = '$columnNoQ'
					AND ccu.column_name = '$targetColumnNoQ'
					";
	
			
			$row = $this->adapter->getRow($sql);
			
			$flagAddKey = false;
			
			if (!$row) $flagAddKey = true;
			
			if ($row) { 
				if (($row['delete_rule']=='SET NULL' && $isDep) || 
					($row['delete_rule']!='SET NULL' && !$isDep)) {
					//delete old key
					$flagAddKey = true; //and order a new one
					$cName = $row['constraint_name'];
					$sql = "ALTER TABLE $table DROP CONSTRAINT $cName ";
					$this->adapter->exec($sql);
				} 
				
			}
			
			if ($flagAddKey) {
			$delRule = ($isDep ? 'CASCADE' : 'SET NULL');	
			$this->adapter->exec("ALTER TABLE  $table
					ADD FOREIGN KEY (  $column ) REFERENCES  $targetTable (
					$targetColumn) ON DELETE $delRule ON UPDATE SET NULL DEFERRABLE ;");
					return true;
			}
			return false;
			
		}
		catch(Exception $e){ return false; }
	}
public function addFK( $type, $targetType, $field, $targetField, $isDep = false) {
		try{
			$table = $this->safeTable($type);
			$column = $this->safeColumn($field);
			$tableNoQ = $this->safeTable($type,true);
			$columnNoQ = $this->safeColumn($field,true);
			$targetTable = $this->safeTable($targetType);
			$targetTableNoQ = $this->safeTable($targetType,true);
			$targetColumn  = $this->safeColumn($targetField);
			$targetColumnNoQ  = $this->safeColumn($targetField,true);
			
			
			$sql = "SELECT
					tc.constraint_name, 
					tc.table_name, 
					kcu.column_name, 
					ccu.table_name AS foreign_table_name,
					ccu.column_name AS foreign_column_name,
					rc.delete_rule
					FROM 
					information_schema.table_constraints AS tc 
					JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
					JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
					JOIN information_schema.referential_constraints AS rc ON ccu.constraint_name = rc.constraint_name
					WHERE constraint_type = 'FOREIGN KEY' AND tc.table_catalog=current_database()
					AND tc.table_name = '$tableNoQ' 
					AND ccu.table_name = '$targetTableNoQ'
					AND kcu.column_name = '$columnNoQ'
					AND ccu.column_name = '$targetColumnNoQ'
					";
	
			
			$row = $this->adapter->getRow($sql);
			
			$flagAddKey = false;
			
			if (!$row) $flagAddKey = true;
			
			if ($row) { 
				if (($row['delete_rule']=='SET NULL' && $isDep) || 
					($row['delete_rule']!='SET NULL' && !$isDep)) {
					//delete old key
					$flagAddKey = true; //and order a new one
					$cName = $row['constraint_name'];
					$sql = "ALTER TABLE $table DROP CONSTRAINT $cName ";
					$this->adapter->exec($sql);
				} 
				
			}
			
			if ($flagAddKey) {
			$delRule = ($isDep ? 'CASCADE' : 'SET NULL');	
			$this->adapter->exec("ALTER TABLE  $table
					ADD FOREIGN KEY (  $column ) REFERENCES  $targetTable (
					$targetColumn) ON DELETE $delRule ON UPDATE SET NULL DEFERRABLE ;");
					return true;
			}
			return false;
			
		}
		catch(Exception $e){ return false; }
	}
public function addFK ( $ type , $ targetType , $ field , $ targetField , $ isDep = false ) { try { $ table = $ this -> safeTable ( $ type ) ; $ column = $ this -> safeColumn ( $ field ) ; $ tableNoQ = $ this -> safeTable ( $ type , true ) ; $ columnNoQ = $ this -> safeColumn ( $ field , true ) ; $ targetTable = $ this -> safeTable ( $ targetType ) ; $ targetTableNoQ = $ this -> safeTable ( $ targetType , true ) ; $ targetColumn = $ this -> safeColumn ( $ targetField ) ; $ targetColumnNoQ = $ this -> safeColumn ( $ targetField , true ) ; $ sql = "SELECT
					tc.constraint_name, 
					tc.table_name, 
					kcu.column_name, 
					ccu.table_name AS foreign_table_name,
					ccu.column_name AS foreign_column_name,
					rc.delete_rule
					FROM 
					information_schema.table_constraints AS tc 
					JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
					JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
					JOIN information_schema.referential_constraints AS rc ON ccu.constraint_name = rc.constraint_name
					WHERE constraint_type = 'FOREIGN KEY' AND tc.table_catalog=current_database()
					AND tc.table_name = '$tableNoQ' 
					AND ccu.table_name = '$targetTableNoQ'
					AND kcu.column_name = '$columnNoQ'
					AND ccu.column_name = '$targetColumnNoQ'
					" ; $ row = $ this -> adapter -> getRow ( $ sql ) ; $ flagAddKey = false ; if ( ! $ row ) $ flagAddKey = true ; if ( $ row ) { if ( ( $ row [ 'delete_rule' ] == 'SET NULL' && $ isDep ) || ( $ row [ 'delete_rule' ] != 'SET NULL' && ! $ isDep ) ) { //delete old key $ flagAddKey = true ; //and order a new one $ cName = $ row [ 'constraint_name' ] ; $ sql = "ALTER TABLE $table DROP CONSTRAINT $cName " ; $ this -> adapter -> exec ( $ sql ) ; } } if ( $ flagAddKey ) { $ delRule = ( $ isDep ? 'CASCADE' : 'SET NULL' ) ; $ this -> adapter -> exec ( "ALTER TABLE  $table
					ADD FOREIGN KEY (  $column ) REFERENCES  $targetTable (
					$targetColumn) ON DELETE $delRule ON UPDATE SET NULL DEFERRABLE ;" ) ; return true ; } return false ; } catch ( Exception $ e ) { return false ; } }
Adds a foreign key to a table. The foreign key will not have any action; you
may configure this afterwards.

@param  string $type        type you want to modify table of
@param  string $targetType  target type
@param  string $field       field of the type that needs to get the fk
@param  string $targetField field where the fk needs to point to

@return bool $success whether an FK has been added
Adds a foreign key to a table . The foreign key will not have any action ; you may configure this afterwards .