diff --git a/src/Builders/Field.php b/src/Builders/Field.php index a187c93..7a87690 100644 --- a/src/Builders/Field.php +++ b/src/Builders/Field.php @@ -50,7 +50,12 @@ class Field implements Buildable /** * @var FieldBuilder */ - protected $builder; + protected $fieldBuilder; + + /** + * @var ClassMetadataBuilder + */ + protected $metaDatabuilder; /** * @var ClassMetadataInfo @@ -70,17 +75,18 @@ class Field implements Buildable /** * Protected constructor to force usage of factory method. * - * @param FieldBuilder $builder - * @param ClassMetadataInfo $classMetadata - * @param Type $type - * @param string $name + * @param FieldBuilder $fieldBuilder + * @param ClassMetadataBuilder $builder + * @param Type $type + * @param string $name */ - protected function __construct(FieldBuilder $builder, ClassMetadataInfo $classMetadata, Type $type, $name) + protected function __construct(FieldBuilder $fieldBuilder, ClassMetadataBuilder $builder, Type $type, $name) { - $this->builder = $builder; - $this->classMetadata = $classMetadata; - $this->type = $type; - $this->name = $name; + $this->fieldBuilder = $fieldBuilder; + $this->metaDatabuilder = $builder; + $this->classMetadata = $builder->getClassMetadata(); + $this->type = $type; + $this->name = $name; } /** @@ -89,7 +95,6 @@ protected function __construct(FieldBuilder $builder, ClassMetadataInfo $classMe * @param string $name * * @throws \Doctrine\DBAL\DBALException - * * @return Field */ public static function make(ClassMetadataBuilder $builder, $type, $name) @@ -98,7 +103,7 @@ public static function make(ClassMetadataBuilder $builder, $type, $name) $field = $builder->createField($name, $type->getName()); - return new static($field, $builder->getClassMetadata(), $type, $name); + return new static($field, $builder, $type, $name); } /** @@ -157,7 +162,7 @@ public function autoIncrement() */ public function generatedValue(callable $callback = null) { - $generatedValue = new GeneratedValue($this->builder, $this->classMetadata); + $generatedValue = new GeneratedValue($this->fieldBuilder, $this->classMetadata); if ($callback) { $callback($generatedValue); @@ -176,7 +181,7 @@ public function generatedValue(callable $callback = null) */ public function unsigned() { - $this->builder->option('unsigned', true); + $this->fieldBuilder->option('unsigned', true); return $this; } @@ -191,7 +196,7 @@ public function unsigned() */ public function fixed($fixed) { - $this->builder->option('fixed', $fixed); + $this->fieldBuilder->option('fixed', $fixed); return $this; } @@ -205,7 +210,7 @@ public function fixed($fixed) */ public function comment($comment) { - $this->builder->option('comment', $comment); + $this->fieldBuilder->option('comment', $comment); return $this; } @@ -219,7 +224,7 @@ public function comment($comment) */ public function collation($collation) { - $this->builder->option('collation', $collation); + $this->fieldBuilder->option('collation', $collation); return $this; } @@ -229,7 +234,28 @@ public function collation($collation) */ public function primary() { - $this->builder->makePrimaryKey(); + $this->fieldBuilder->makePrimaryKey(); + + return $this; + } + + /** + * @param null $name + * + * @return Field + */ + public function index($name = null) + { + $index = new Index( + $this->metaDatabuilder, + [$this->getName()] + ); + + if ($name !== null) { + $index->name($name); + } + + $this->callbackAndQueue($index); return $this; } @@ -239,7 +265,7 @@ public function primary() */ public function useForVersioning() { - $this->builder->isVersionField(); + $this->fieldBuilder->isVersionField(); return $this; } @@ -249,7 +275,7 @@ public function useForVersioning() */ public function build() { - $this->builder->build(); + $this->fieldBuilder->build(); $this->buildQueued(); @@ -261,7 +287,7 @@ public function build() */ public function getBuilder() { - return $this->builder; + return $this->fieldBuilder; } /** @@ -271,7 +297,6 @@ public function getBuilder() * @param array $args * * @throws BadMethodCallException - * * @return $this */ public function __call($method, $args) @@ -303,7 +328,7 @@ public function __call($method, $args) */ protected function setDefault($default) { - $this->builder->option('default', $default); + $this->fieldBuilder->option('default', $default); return $this; } diff --git a/tests/Builders/FieldTest.php b/tests/Builders/FieldTest.php index c76551e..7c2f168 100644 --- a/tests/Builders/FieldTest.php +++ b/tests/Builders/FieldTest.php @@ -30,6 +30,7 @@ class FieldTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->builder = new ClassMetadataBuilder(new ClassMetadataInfo(StubEntity::class)); + $this->builder->setTable('stub_entities'); $this->field = Field::make($this->builder, 'string', 'name'); } @@ -48,6 +49,32 @@ public function test_can_make_field_nullable() $this->assertTrue($this->builder->getClassMetadata()->getFieldMapping('name')['nullable']); } + public function test_can_make_field_index() + { + $this->field->index(); + + $this->field->build(); + + $indexes = $this->builder->getClassMetadata()->table['indexes']; + + $this->assertArrayHasKey('stub_entities_name_index', $indexes); + $this->assertCount(1, $indexes['stub_entities_name_index']['columns']); + $this->assertContains('name', $indexes['stub_entities_name_index']['columns']); + } + + public function test_can_make_field_index_with_custom_name() + { + $this->field->index('index_name'); + + $this->field->build(); + + $indexes = $this->builder->getClassMetadata()->table['indexes']; + + $this->assertArrayHasKey('index_name', $indexes); + $this->assertCount(1, $indexes['index_name']['columns']); + $this->assertContains('name', $indexes['index_name']['columns']); + } + public function test_can_set_column_name() { $this->field->columnName('name_column');