ZendDbSchema
Schema management for Zend Framework
 All Classes Namespaces Functions Pages
ForeignKey.php
1 <?php
31 {
32  const COLUMNS_KEY = 'columns';
33  const REFERENCE_KEY = 'references';
34  const ONDELETE_KEY = 'delete';
35  const ONUPDATE_KEY = 'update';
36 
42  protected function _getSchemaKey()
43  {
44  return ZendDbSchema_Db_Schema_Table::FOREIGNS_KEY;
45  }
46 
50  protected function _getDropSql()
51  {
52  return $this->getTable()->getGenerator()->dropForeignKey($this);
53  }
54 
58  protected function _getAlterSql()
59  {
60  return $this->getTable()->getGenerator()->alterForeignKey($this);
61  }
62 
66  protected function _getCreateSql()
67  {
68  return $this->getTable()->getGenerator()->addForeignKey($this);
69  }
70 
77  public function setReferences($table)
78  {
79  if (!$table instanceof ZendDbSchema_Db_Schema_Table) {
80  $table = new ZendDbSchema_Db_Schema_Table($table);
81  }
82 
83  if (!$table->isExist()) {
84  throw new ZendDbSchema_Db_Schema_Exception('Cannot set reference to not existing table');
85  }
86 
87  $this->__set(self::REFERENCE_KEY, $table->getOriginName());
88  return $this;
89  }
90 
96  public function getReferences()
97  {
98  return $this->__get(self::REFERENCE_KEY);
99  }
100 
107  public function setColumns(array $columns)
108  {
109  $this->__set(self::COLUMNS_KEY, $columns);
110  return $this;
111  }
112 
119  public function hasColumn($columnName)
120  {
121  return array_key_exists($columnName, $this->getColumns());
122  }
123 
130  public function hasRefColumn($refColumnName)
131  {
132  $columns = $this->getColumns();
133  return in_array($refColumnName, $columns);
134  }
135 
143  public function setColumn($columnName, $refColumnName)
144  {
145  $columns = $this->getColumns();
146  $columns[$columnName] = $refColumnName;
147  $this->setColumns($columns);
148 
149  return $this;
150  }
151 
157  public function getColumns()
158  {
159  $columns = $this->__get(self::COLUMNS_KEY);
160 
161  return is_array($columns) ? $columns : array();
162  }
163 
170  public function removeColumn($columnName)
171  {
172  if ($this->hasColumn($columnName)) {
173  $columns = $this->getColumns();
174  unset($columns[$columnName]);
175  $this->setColumns($columns);
176  }
177  return $this;
178  }
179 
186  public function setOnDelete($onDelete)
187  {
188  $this->__set(self::ONDELETE_KEY, $onDelete);
189  return $this;
190  }
191 
197  public function getOnDelete()
198  {
199  return $this->__get(self::ONDELETE_KEY);
200  }
201 
202 
209  public function setOnUpdate($onUpdate)
210  {
211  $this->__set(self::ONUPDATE_KEY, $onUpdate);
212  return $this;
213  }
214 
220  public function getOnUpdate()
221  {
222  return $this->__get(self::ONUPDATE_KEY);
223  }
224 }