From dcccbce54ad8957a92d538b69a7276d9e7336346 Mon Sep 17 00:00:00 2001 From: Anton Okulov Date: Sat, 8 Feb 2020 13:47:57 +0300 Subject: [PATCH] Added indexes to Model. Now, you can control it! --- src/Phact/Orm/Index.php | 97 ++++++++++++++++++++++++++++++++++ src/Phact/Orm/Model.php | 18 +++++-- src/Phact/Orm/TableManager.php | 35 +++++++++--- 3 files changed, 141 insertions(+), 9 deletions(-) create mode 100644 src/Phact/Orm/Index.php diff --git a/src/Phact/Orm/Index.php b/src/Phact/Orm/Index.php new file mode 100644 index 0000000..7111af3 --- /dev/null +++ b/src/Phact/Orm/Index.php @@ -0,0 +1,97 @@ +indexName = $indexName; + $this->columns = $columns; + $this->isUnique = $isUnique; + $this->isPrimary = $isPrimary; + $this->flags = $flags; + $this->options = $options; + } + + /** + * @return mixed + */ + public function getIndexName() + { + return $this->indexName; + } + + /** + * @return array + */ + public function getColumns(): array + { + return $this->columns; + } + + /** + * @return bool + */ + public function isUnique(): bool + { + return $this->isUnique; + } + + /** + * @return bool + */ + public function isPrimary(): bool + { + return $this->isPrimary; + } + + /** + * @return array + */ + public function getFlags(): array + { + return $this->flags; + } + + /** + * @return array + */ + public function getOptions(): array + { + return $this->options; + } +} \ No newline at end of file diff --git a/src/Phact/Orm/Model.php b/src/Phact/Orm/Model.php index e6ebce7..12374e1 100644 --- a/src/Phact/Orm/Model.php +++ b/src/Phact/Orm/Model.php @@ -13,16 +13,13 @@ namespace Phact\Orm; use InvalidArgumentException; -use Phact\Event\EventManager; use Phact\Event\EventManagerInterface; use Phact\Exceptions\UnknownMethodException; use Phact\Helpers\ClassNames; use Phact\Helpers\SmartProperties; use Phact\Helpers\Text; -use Phact\Main\Phact; use Phact\Orm\Configuration\ConfigurationProvider; use Phact\Orm\Fields\Field; -use Phact\Orm\Fields\RelationField; use Serializable; /** @@ -171,6 +168,21 @@ public static function getFields() return []; } + /** + * @return Index[] + */ + public function getIndexes() + { + $indexes = []; + + $pkAttribute = $this->getPkAttribute(); + if ($pkAttribute) { + $indexes[] = new Index($pkAttribute, [$pkAttribute], true, true); + } + + return $indexes; + } + public static function getMetaData() { return [ diff --git a/src/Phact/Orm/TableManager.php b/src/Phact/Orm/TableManager.php index cf561aa..8217506 100644 --- a/src/Phact/Orm/TableManager.php +++ b/src/Phact/Orm/TableManager.php @@ -15,10 +15,10 @@ use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\ForeignKeyConstraint; -use Doctrine\DBAL\Schema\Index; +use Doctrine\DBAL\Schema\Index as DBALIndex; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Types\Type; -use Phact\Main\Phact; +use Phact\Orm\Index as PhactIndex; use Phact\Orm\Configuration\ConfigurationProvider; use Phact\Orm\Fields\Field; use Phact\Orm\Fields\ManyToManyField; @@ -84,11 +84,10 @@ public function createModelTable($model) { $tableName = $model->getTableName(); $columns = $this->createColumns($model); - $attribute = $model->getPkAttribute(); - $table = new Table($tableName, $columns, [ - new Index($attribute, [$attribute], true, true) - ]); + $dbalIndexes = $this->convertIndexes($model->getIndexes()); + + $table = new Table($tableName, $columns, $dbalIndexes); $schemaManager = $this->getSchemaManager($model); if (!$schemaManager->tablesExist([$tableName])) { $schemaManager->createTable($table); @@ -217,4 +216,28 @@ public function dropModelForeignKeys($model) $schemaManager->dropForeignKey($constraint, $tableName); } } + + /** + * @param PhactIndex[] $indexes + * @return DBALIndex[] + * @throws \Exception + */ + public function convertIndexes(array $indexes) + { + $dbalIndexes = []; + foreach ($indexes as $index) { + if (!($index instanceof PhactIndex)) { + throw new \Exception('Invalid index object. Expected ' . PhactIndex::class); + } + $dbalIndexes[] = new DBALIndex( + $index->getIndexName(), + $index->getColumns(), + $index->isUnique(), + $index->isPrimary(), + $index->getFlags(), + $index->getOptions() + ); + } + return $dbalIndexes; + } } \ No newline at end of file