function toDialectString(IDialect $dialect)
 {
     $table = $dialect->quoteIdentifier($this->constraint->getTable()->getName());
     $ct = $dialect->quoteIdentifier($this->constraint->getName());
     if ($dialect->getDBDriver()->is(DBDriver::MYSQL)) {
         if ($this->constraint instanceof DBUniqueConstraint) {
             return 'ALTER TABLE ' . $table . ' DROP INDEX ' . $ct;
         } else {
             if ($this->constraint instanceof DBPrimaryKeyConstraint) {
                 return 'ALTER TABLE ' . $table . ' DROP PRIMARY KEY';
             } else {
                 if ($this->constraint instanceof DBOneToOneConstraint || $this->constraint instanceof DBForeignKeyConstraint) {
                     return 'ALTER TABLE ' . $table . ' DROP FOREIGN KEY ' . $ct;
                 }
             }
         }
         Assert::isUnreachable('Do not know how to remove constraint %s from MySQL', get_class($this->constraint));
     }
     return 'ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $ct . ' CASCADE;';
 }
 /**
  * @param array $fields fields of referencing table that reference primary another table
  * @param DBTable $referencedTable object that represents referenced table
  * @param AssociationBreakAction $associationBreakAction action which is performed on reference break
  */
 function __construct($name, DBTable $table, array $fields, DBTable $referencedTable, AssociationBreakAction $associationBreakAction)
 {
     foreach ($referencedTable->getConstraints() as $constraint) {
         if ($constraint instanceof DBPrimaryKeyConstraint) {
             $pkFields = $constraint->getFields();
             Assert::isTrue(sizeof($pkFields) == sizeof($fields), 'foreign key (%s) should have the same number of columns as %s`s table primary key (%s)', join(', ', $fields), $referencedTable->getName(), join(', ', $pkFields));
             $this->fields = new SqlFieldArray($fields);
             $this->pkFields = new SqlFieldArray($pkFields);
             $this->referencedTable = $referencedTable;
             $this->associationBreakAction = $associationBreakAction;
             parent::__construct($name, $table, $fields);
             return;
         }
     }
     Assert::isUnreachable('referenced table `%s` MUST contain DBPrimaryKeyConstraint', $referencedTable->getName());
 }
Ejemplo n.º 3
0
 /**
  * Adds a table constraint
  *
  * @param DBConstraint $constraint constraint to add
  * @throws DuplicationException thrown when another constaint with the same name already added
  * @return DBTable itself
  */
 function addConstraint(DBConstraint $constraint)
 {
     $name = $constraint->getName();
     if ($name) {
         if (isset($this->constraints[$name])) {
             throw new DuplicationException('constraint', $name);
         }
     } else {
         $name = 'constraint_' . $this->name . '_' . (sizeof($this->constraints) + 1);
         $constraint->setName($name);
     }
     $this->constraints[$name] = $constraint;
     return $this;
 }
 function toDialectString(IDialect $dialect)
 {
     return 'ALTER TABLE ' . $dialect->quoteIdentifier($this->constraint->getTable()->getName()) . ' ADD ' . $this->constraint->toDialectString($dialect) . ';';
 }
 protected function getHead(IDialect $dialect)
 {
     return parent::getHead($dialect) . ' UNIQUE';
 }
Ejemplo n.º 6
0
 /**
  * Adds a table constraint
  *
  * @param DBConstraint $constraint constraint to add
  * @throws DuplicationException thrown when another constaint with the same name already added
  * @return DBTable itself
  */
 function addConstraint(DBConstraint $constraint)
 {
     $name = $constraint->getName();
     if ($name) {
         if (isset($this->constraints[$name])) {
             throw new DuplicationException('constraint', $name);
         }
     } else {
         $name = 'constraint_' . join('_', $constraint->getFields()) . (sizeof($this->constraints) + 1);
         $constraint->setName($name);
     }
     $this->constraints[$name] = $constraint;
     if ($constraint instanceof DBPrimaryKeyConstraint) {
         if ($this->pk) {
             throw new DuplicationException('constraint', $name);
         }
         $this->pk = $constraint;
     }
     return $this;
 }