ZendDbSchema
Schema management for Zend Framework
 All Classes Namespaces Functions Pages
PrimaryKey.php
1 <?php
2 
5 {
6  const COLUMNS_KEY = 'columns';
7  const TYPE_KEY = 'type';
8 
14  protected function _getSchemaKey()
15  {
16  return ZendDbSchema_Db_Schema_Table::PRIMARY_KEY;
17  }
18 
22  protected function _getDropSql()
23  {
24  return $this->getTable()->getGenerator()->dropPrimaryKey($this);
25  }
26 
30  protected function _getAlterSql()
31  {
32  return $this->getTable()->getGenerator()->alterPrimaryKey($this);
33  }
34 
38  protected function _getCreateSql()
39  {
40  return $this->getTable()->getGenerator()->addPrimaryKey($this);
41  }
42 
50  public function __construct(ZendDbSchema_Db_Schema_Table $table = null)
51  {
52  if ($table) {
53  $this->_table = $table;
54  $this->refresh();
55  }
56 
57  $this->init();
58  }
59 
65  protected function _getCleanSchema()
66  {
67  $schema = array();
68  if ($this->getTable()) {
69  $data = $this->getTable()->getCleanSchema();
70 
71  if (isset($data[$this->_getSchemaKey()])) {
72  $schema = $data[$this->_getSchemaKey()];
73  }
74  }
75  return $schema;
76  }
77 
84  public function setColumns(array $columns)
85  {
86  $this->__set(self::COLUMNS_KEY, array_unique($columns));
87  return $this;
88  }
89 
96  public function hasColumn($columnName)
97  {
98  return in_array($columnName, $this->getColumns());
99  }
100 
107  public function addColumn($columnName)
108  {
109  if (!$this->hasColumn($columnName)) {
110  $columns = $this->getColumns();
111  $columns[] = $columnName;
112  $this->setColumns($columns);
113  }
114  return $this;
115  }
116 
122  public function getColumns()
123  {
124  $columns = $this->__get(self::COLUMNS_KEY);
125  return is_array($columns) ? $columns : array();
126  }
127 
134  public function removeColumn($columnName)
135  {
136  if ($this->hasColumn($columnName)) {
137  $columns = array_flip($this->getColumns());
138  unset($columns[$columnName]);
139  $this->setColumns(array_flip($columns));
140  }
141  return $this;
142  }
143 }