Contao\CoreBundle\EventListener\DoctrineSchemaListener::onSchemaIndexDefinition PHP Method

onSchemaIndexDefinition() public method

Handles the Doctrine schema and overrides the indexes with a fixed length.
public onSchemaIndexDefinition ( Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs $event )
$event Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs
    public function onSchemaIndexDefinition(SchemaIndexDefinitionEventArgs $event)
    {
        $connection = $event->getConnection();
        $data = $event->getTableIndex();
        if (!$connection->getDatabasePlatform() instanceof MySqlPlatform || 'PRIMARY' === $data['name']) {
            return;
        }
        $index = $connection->fetchAssoc(sprintf("SHOW INDEX FROM %s WHERE Key_name='%s'", $event->getTable(), $data['name']));
        if (null !== $index['Sub_part']) {
            $columns = [];
            foreach ($data['columns'] as $col) {
                $columns[$col] = sprintf('%s(%s)', $col, $index['Sub_part']);
            }
            $event->setIndex(new Index($data['name'], $columns, $data['unique'], $data['primary'], $data['flags'], $data['options']));
            $event->preventDefault();
        }
    }

Usage Example

 /**
  * Tests that the onSchemaIndexDefinition() method ignores non MySQL platforms.
  */
 public function testOnSchemaIndexDefinitionIgnoresNonMySqlPlatform()
 {
     /** @var Connection|\PHPUnit_Framework_MockObject_MockObject $event */
     $connection = $this->getMock('Doctrine\\DBAL\\Connection', ['getDatabasePlatform', 'fetchAssoc'], [], '', false);
     $connection->expects($this->any())->method('getDatabasePlatform')->willReturn(new PostgreSqlPlatform());
     $connection->expects($this->never())->method('fetchAssoc');
     /** @var SchemaIndexDefinitionEventArgs|\PHPUnit_Framework_MockObject_MockObject $event */
     $event = $this->getMock('Doctrine\\DBAL\\Event\\SchemaIndexDefinitionEventArgs', [], [], '', false);
     $event->expects($this->any())->method('getConnection')->willReturn($connection);
     $event->expects($this->any())->method('getTableIndex')->willReturn($this->getIndexEventArg('pid'));
     $event->expects($this->never())->method('setIndex');
     $listener = new DoctrineSchemaListener($this->getMock('Contao\\CoreBundle\\Doctrine\\Schema\\DcaSchemaProvider', [], [], '', false));
     $listener->onSchemaIndexDefinition($event);
 }