/** * Add one field to the table, allowing to specify the desired order * If it's not specified, then the field is added at the end * @param xmldb_field $field * @param xmldb_object $after * @return xmldb_field */ public function addField($field, $after = null) { // Detect duplicates first if ($this->getField($field->getName())) { throw new coding_exception('Duplicate field ' . $field->getName() . ' specified in table ' . $this->getName()); } // Calculate the previous and next fields $prevfield = null; $nextfield = null; if (!$after) { $allfields = $this->getFields(); if (!empty($allfields)) { end($allfields); $prevfield = $allfields[key($allfields)]; } } else { $prevfield = $this->getField($after); } if ($prevfield && $prevfield->getNext()) { $nextfield = $this->getField($prevfield->getNext()); } // Set current field previous and next attributes if ($prevfield) { $field->setPrevious($prevfield->getName()); $prevfield->setNext($field->getName()); } if ($nextfield) { $field->setNext($nextfield->getName()); $nextfield->setPrevious($field->getName()); } // Some more attributes $field->setLoaded(true); $field->setChanged(true); // Add the new field $this->fields[] = $field; // Reorder the field $this->orderFields($this->fields); // Recalculate the hash $this->calculateHash(true); // We have one new field, so the table has changed $this->setChanged(true); return $field; }