ZendDbSchema
Schema management for Zend Framework
 All Classes Namespaces Functions Pages
Table.php
1 <?php
30 {
31  const CHARSET_KEY = 'charset';
32 
33  const COLUMNS_KEY = 'columns';
34  const INDEXES_KEY = 'indexes';
35  const FOREIGNS_KEY = 'foreigns';
36  const PRIMARY_KEY = 'primary';
37 
41  protected $_columnsManagerClass = 'ZendDbSchema_Db_Schema_Table_DefinitionManager_Column';
42 
46  protected $_indexesManagerClass = 'ZendDbSchema_Db_Schema_Table_DefinitionManager_Index';
47 
51  protected $_foreignsManagerClass = 'ZendDbSchema_Db_Schema_Table_DefinitionManager_ForeignKey';
52 
56  protected $_columnClass = 'ZendDbSchema_Db_Schema_Table_Column';
57 
61  protected $_indexClass = 'ZendDbSchema_Db_Schema_Table_Index';
62 
66  protected $_foreignClass = 'ZendDbSchema_Db_Schema_Table_ForeignKey';
67 
71  protected $_primaryKeyClass = 'ZendDbSchema_Db_Schema_Table_PrimaryKey';
72 
78  protected $_columns;
79 
85  protected $_indexes;
86 
92  protected $_foreigns;
93 
99  protected $_primary;
100 
104  protected $_loader;
105 
115  public function __construct(
116  $name = null,
119  ZendDbSchema_Db_Schema_Generator_Table $generator = null)
120  {
121  if (!$adapter) {
122  $adapter = self::getDefaultAdapter();
123  }
124 
125  if (!$loader || !$generator) {
126  switch (true) {
127  case $adapter instanceof Zend_Db_Adapter_Mysqli:
128  case $adapter instanceof Zend_Db_Adapter_Pdo_Mysql:
129  if (!$loader) {
131  }
132  if (!$generator) {
134  }
135  break;
136  }
137  }
138  //TODO check that adapter, generator and loader are same type
139 
140  $this->_generator = $generator;
141  $this->_loader = $loader;
142  $this->_adapter = $adapter;
143 
144  parent::__construct($name);
145  }
146 
153  public function __set($name, $value)
154  {
155  if (self::NAME_KEY == $name && $value) {
156  if ($this->_name != $value && $this->getAdapter()->hasTable($value)) {
157  throw new ZendDbSchema_Db_Schema_Exception("Table with name '{$value}' already exists");
158  }
159  }
160  parent::__set($name, $value);
161  }
162 
167  public function __clone()
168  {
169  parent::__clone();
170 
171  $schema = $this->toArray();
172  $this->_indexes = $this->_columns = $this->_primary = $this->_foreigns = null;
173 
174  $this->setFromArray($schema);
175  }
176 
182  public function getColumnClass()
183  {
184  return $this->_columnClass;
185  }
186 
192  public function getIndexClass()
193  {
194  return $this->_indexClass;
195  }
196 
202  public function getForeignClass()
203  {
204  return $this->_foreignClass;
205  }
206 
213  public function setFromArray(array $schema)
214  {
215  $columns = array();
216  if (!empty($schema[self::COLUMNS_KEY])) {
217  $columns = $schema[self::COLUMNS_KEY];
218  }
219  $this->columns()->setFromArray($columns);
220 
221  $index = array();
222  if (!empty($schema[self::INDEXES_KEY])) {
223  $index = $schema[self::INDEXES_KEY];
224  }
225  $this->indexes()->setFromArray($index);
226 
227  $foreign = array();
228  if (!empty($schema[self::FOREIGNS_KEY])) {
229  $foreign = $schema[self::FOREIGNS_KEY];
230  }
231  $this->foreigns()->setFromArray($foreign);
232 
233  $primary = array();
234  if (!empty($schema[self::PRIMARY_KEY])) {
235  $primary = $schema[self::PRIMARY_KEY];
236  }
237  $this->primary()->setFromArray($primary);
238 
239  unset(
240  $schema[self::COLUMNS_KEY],
241  $schema[self::INDEXES_KEY],
242  $schema[self::FOREIGNS_KEY],
243  $schema[self::PRIMARY_KEY]
244  );
245 
246  return parent::setFromArray($schema);
247  }
248 
254  public function toArray()
255  {
256  $schema = parent::toArray();
257 
258  $schema[self::COLUMNS_KEY] = $this->columns()->toArray();
259  $schema[self::PRIMARY_KEY] = $this->primary()->toArray();
260  $schema[self::INDEXES_KEY] = $this->indexes()->toArray();
261  $schema[self::FOREIGNS_KEY] = $this->foreigns()->toArray();
262 
263  return $schema;
264  }
265 
271  public function columns()
272  {
273  if (!$this->_columns) {
274  $this->_columns = new $this->_columnsManagerClass($this);
275  }
276  return $this->_columns;
277  }
278 
285  public function primary(ZendDbSchema_Db_Schema_Table_PrimaryKey $primary = null)
286  {
287  if ($primary) {
288  $this->_primary = $primary;
289  } elseif (!$this->_primary) {
290  $this->_primary = new $this->_primaryKeyClass($this);
291  }
292  return $this->_primary;
293  }
294 
300  public function indexes()
301  {
302  if (!$this->_indexes) {
303  $this->_indexes = new $this->_indexesManagerClass($this);
304  }
305  return $this->_indexes;
306  }
307 
313  public function foreigns()
314  {
315  if (!$this->_foreigns) {
316  $this->_foreigns = new $this->_foreignsManagerClass($this);
317  }
318  return $this->_foreigns;
319  }
320 
327  public function toSql($alter = false)
328  {
329  if ($alter && (!$this->isExist() || !$this->isDirty())) {
330  return '';
331  }
332  $query = array();
333  $query[] = parent::toSql($alter);
334 
335  foreach ($this->columns() as $column) {
336  if ($alter && $column->isDirty()) {
337  $query[] = $column->toSql($alter);
338  }
339  }
340  foreach ($this->indexes() as $index) {
341  if (!$alter || $index->isDirty()) {
342  $query[] = $index->toSql($alter);
343  }
344  }
345 
346  if (!$alter || $this->primary()->isDirty()) {
347  $query[] = $this->primary()->toSql($alter);
348  }
349 
350  foreach ($this->foreigns() as $foreignKey) {
351  if (!$alter || $foreignKey->isDirty()) {
352  $query[] = $foreignKey->toSql($alter);
353  }
354  }
355 
356  return join(';' . PHP_EOL, array_filter($query));
357  }
358 
365  public function isDirty($param = null)
366  {
367  $clean = $this->getCleanSchema();
368 
369  if ($param) {
370  if (!array_key_exists($param, $clean)) {
371  return $this->__isset($param);
372  }
373  return $this->getCleanSchema($param) != $this->__get($param);
374  }
375 
376  unset(
377  $clean[self::COLUMNS_KEY],
378  $clean[self::INDEXES_KEY],
379  $clean[self::PRIMARY_KEY],
380  $clean[self::FOREIGNS_KEY]
381  );
382 
383  if (count($clean) != count($this->_params)) {
384  return true;
385  }
386  foreach ($this->_params as $paramName => $paramValue) {
387  if ($this->isDirty($paramName)) {
388  return true;
389  }
390  }
391 
392  if ($this->columns()->isDirty()
393  || $this->indexes()->isDirty()
394  || $this->foreigns()->isDirty()
395  || $this->primary()->isDirty()) {
396  return true;
397  }
398  return false;
399  }
400 
404  protected function _getAlterSql()
405  {
406  return $this->getGenerator()->alterTable($this);
407  }
408 
412  protected function _getCreateSql()
413  {
414  return $this->getGenerator()->createTable($this);
415  }
416 
420  protected function _getDropSql()
421  {
422  return $this->getGenerator()->dropTable($this);
423  }
424 
430  protected function _doReload()
431  {
432  $this->_cleanSchema = $this->_loader->load(
433  $this->getAdapter(),
434  $this->getOriginName()
435  );
436  }
437 }