Skip to content

Commit

Permalink
Added indexes to Model. Now, you can control it!
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonOkulov committed Feb 8, 2020
1 parent 07d82d2 commit dcccbce
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 9 deletions.
97 changes: 97 additions & 0 deletions src/Phact/Orm/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
*
*
* All rights reserved.
*
* @author Okulov Anton
* @email [email protected]
* @version 1.0
* @date 08/02/2020 11:39
*/

namespace Phact\Orm;


class Index
{
private $indexName;
/**
* @var array
*/
private $columns;
/**
* @var bool
*/
private $isUnique;
/**
* @var bool
*/
private $isPrimary;
/**
* @var array
*/
private $flags;
/**
* @var array
*/
private $options;

public function __construct($indexName, array $columns, $isUnique = false, $isPrimary = false, array $flags = [], array $options = [])
{
$this->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;
}
}
18 changes: 15 additions & 3 deletions src/Phact/Orm/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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 [
Expand Down
35 changes: 29 additions & 6 deletions src/Phact/Orm/TableManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}

0 comments on commit dcccbce

Please sign in to comment.