/** * Checks if the provided index has the same fields as this index * @param Index $index Index to check * @return boolean True when the provided index has the same fields, false otherwise */ public function equals(Index $index) { $fields = $index->getFields(); foreach ($fields as $fieldName => $field) { if (!isset($this->fields[$fieldName])) { return false; } } foreach ($this->fields as $fieldName => $field) { if (!isset($fields[$fieldName])) { return false; } } return true; }
/** * Checks if the provided index has the same fields as this index * @param Index $index Index to check * @return boolean True when the provided index has the same fields, false otherwise */ public function equals(Index $index) { $fields = $index->getFields(); foreach ($fields as $fieldName => $field) { if (!array_key_exists($fieldName, $this->fields)) { return false; } } foreach ($this->fields as $fieldName => $field) { if (!array_key_exists($fieldName, $fields)) { return false; } } return true; }
/** * Gets the information about the fields of the index * @param zibo\library\orm\definition\Index $index * @return string */ private function getFieldsInfo(Index $index) { $info = ''; $fields = array_keys($index->getFields()); $numFields = count($fields); if ($numFields == 1) { $field = array_pop($fields); $info .= $this->translator->translate('orm.label.index.field.in', array('field' => $field)) . '<br />'; } else { $last = array_pop($fields); $first = implode(', ', $fields); $info .= $this->translator->translate('orm.label.index.fields.in', array('first' => $first, 'last' => $last)) . '<br />'; } return $info; }
/** * Sets or adds a new index definition * @param Index $index Definition of the index to set or add * @return null * @throws zibo\library\database\exception\DatabaseException when the index contains a field which is not in this table */ public function setIndex(Index $index) { $fields = $index->getFields(); foreach ($fields as $field) { if (!$this->hasField($field->getName())) { throw new DatabaseException('Cannot add the index: the field ' . $field->getName() . ' is not set in this table'); } } $this->indexes[$index->getName()] = $index; }
/** * Sets an index to the table * @param zibo\library\database\definition\Index $index index to add to the table * @return null * @throws zibo\library\orm\exception\ModelException when a field of the index is not in this table * @throws zibo\library\orm\exception\ModelException when a field of the index is not a property or a belongs to field * @throws zibo\library\orm\exception\ModelException when the index contains out of localized and unlocalized fields */ public function setIndex(Index $index) { $isLocalized = null; $fields = $index->getFields(); foreach ($fields as $fieldName => $field) { if (!$this->hasField($fieldName)) { throw new ModelException('Cannot add the index: the field ' . $fieldName . ' is not set in this table'); } if ($this->fields[$fieldName] instanceof HasField) { throw new ModelException('Cannot add the index: the field ' . $fieldName . ' is not a property or a belongs to field'); } if ($isLocalized === null) { $isLocalized = $field->isLocalized(); } elseif ($field->isLocalized() != $isLocalized) { throw new ModelException('Cannot combine localized and unlocalized fields in 1 index'); } } $this->indexes[$index->getName()] = $index; }
/** * Adds a index to the provided table * @param string $tableName Quoted name of the table * @param zibo\library\database\definition\Index $index Index to add * @return null */ private function addIndex($tableName, Index $index) { $fields = $index->getFields(); foreach ($fields as $fieldName => $field) { $fields[$fieldName] = $this->connection->quoteIdentifier($fieldName); } $sql = 'ALTER TABLE ' . $tableName . ' ADD INDEX ' . $this->connection->quoteIdentifier($index->getName()) . ' ('; $sql .= implode(', ', $fields) . ')'; $this->connection->execute($sql); }
/** * Gets the index element for the provided index * @param zibo\library\xml\dom\Document $dom * @param zibo\library\database\definition\Index $index Index to get the element from * @return DOMElement */ protected function getElementFromIndex(Document $dom, Index $index) { $indexName = $index->getName(); $indexElement = $dom->createElement(self::TAG_INDEX); $indexElement->setAttribute(self::ATTRIBUTE_NAME, $indexName); $fields = $index->getFields(); foreach ($fields as $field) { $fieldElement = $dom->createElement(self::TAG_INDEX_FIELD); $fieldElement->setAttribute(self::ATTRIBUTE_NAME, $field->getName()); $indexElement->appendChild($fieldElement); } return $indexElement; }
/** * Adds a index to the provided table * @param string $tableName Plain name of the table * @param zibo\library\database\definition\Index $index Index to add * @return null */ private function addIndex($tableName, Index $index) { $fields = $index->getFields(); foreach ($fields as $fieldName => $field) { $fields[$fieldName] = $this->connection->quoteIdentifier($fieldName); } $sql = 'CREATE INDEX ' . $this->connection->quoteIdentifier('index' . ucFirst($tableName) . ucfirst($index->getName())) . ' ON ' . $this->connection->quoteIdentifier($tableName) . ' ('; $sql .= implode(', ', $fields) . ')'; $this->connection->execute($sql); }