Exemplo n.º 1
0
 /**
  * Builds a CONSTRAINT name FOREIGN KEY (field1, field2, .
  *
  *
  *
  *
  *
  *
  * ..) REFERENCES table(referenced1, referenced2, ...) ON DELETE action
  * definition that can be used in CREATE or ALTER statements.
  *
  * @param ForeignKey $foreignKey the foreign key
  * @return string the definition statement
  */
 private function buildForeignKeyDefinition(ForeignKey $foreignKey)
 {
     $res = 'CONSTRAINT ' . $foreignKey->getName();
     $res .= ' FOREIGN KEY (';
     $references = $foreignKey->getReferences();
     $fields = array();
     $referencedFields = array();
     foreach ($references as $row) {
         $fields[] = $row['field'];
         $referencedFields[] = $row['referencedField'];
     }
     $res .= '`' . implode('`,`', $fields) . '`';
     $res .= ') REFERENCES ' . $this->addPrefix($foreignKey->getReferencedTable()) . ' (';
     $res .= '`' . implode('`,`', $referencedFields) . '`';
     $res .= ') ON DELETE ';
     switch ($foreignKey->getOnDeleteAction()) {
         case ForeignKey::ACTION_NOACTION:
             $res .= 'NO ACTION';
             break;
         case ForeignKey::ACTION_RESTRICT:
             $res .= 'RESTRICT';
             break;
         case ForeignKey::ACTION_CASCADE:
             $res .= 'CASCADE';
             break;
         case ForeignKey::ACTION_SETNULL:
             $res .= 'SET NULL';
             break;
     }
     return $res;
 }
Exemplo n.º 2
0
 /**
  * Adds a foreign key to this table.
  *
  * @param ForeignKey $foreignKey the foreign key to add
  */
 public final function addForeignKey(ForeignKey $foreignKey)
 {
     $this->foreignKeys[$foreignKey->getName()] = $foreignKey;
 }