Laminas Framework com Doctrine Migration – Error
Voltei a trabalhar com Zend + Doctrine + Migration, porém tive alguns problemas ao realizar a instalação.
Vou interpretar que você já passou pelos seguintes passos:
composer create-project -s dev laminas/laminas-mvc-skeleton path/to/install composer require doctrine/doctrine-orm-module composer require doctrine/doctrine-migrations
Depois de tentar executar o comando:
vendor\bin\doctrine-module migrations:generate
Me deparei com o seguinte erro:
PHP Warning: assert(): assert(is_string($namespace)) failed in C:\xampp\htdocs\migration\vendor\doctrine\migrations\lib\Doctrine\Migrations\Tools\Console\Command\GenerateCommand.php on line 65 Warning: assert(): assert(is_string($namespace)) failed in C:\xampp\htdocs\migration\vendor\doctrine\migrations\lib\Doctrine\Migrations\Tools\Console\Command\GenerateCommand.php on line 65 PHP Fatal error: Uncaught TypeError: Argument 1 passed to Doctrine\Migrations\Generator\ClassNameGenerator::generateClassName() must be of the type string, null given, called in C:\xampp\htdocs\migration\vendor\doctrine\migrations\lib\Doctrine\Migrations\Tools\Console\Command\GenerateCommand.php on line 67 and defined in C:\xampp\htdocs\migration\vendor\doctrine\migrations\lib\Doctrine\Migrations\Generator\ClassNameGenerator.php:14 Stack trace: #0 C:\xampp\htdocs\migration\vendor\doctrine\migrations\lib\Doctrine\Migrations\Tools\Console\Command\GenerateCommand.php(67): Doctrine\Migrations\Generator\ClassNameGenerator->generateClassName(NULL) #1 C:\xampp\htdocs\migration\vendor\symfony\console\Command\Command.php(299): Doctrine\Migrations\Tools\Console\Command\GenerateCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #2 C:\xampp\htdocs\migration\vendor\symfony\console\Application.php(978): Symfony\Component\Console\Command\Command->run(Object(Symfony\Com in C:\xampp\htdocs\migration\vendor\doctrine\migrations\lib\Doctrine\Migrations\Generator\ClassNameGenerator.php on line 14 Fatal error: Uncaught TypeError: Argument 1 passed to Doctrine\Migrations\Generator\ClassNameGenerator::generateClassName() must be of the type string, null given, called in C:\xampp\htdocs\migration\vendor\doctrine\migrations\lib\Doctrine\Migrations\Tools\Console\Command\GenerateCommand.php on line 67 and defined in C:\xampp\htdocs\migration\vendor\doctrine\migrations\lib\Doctrine\Migrations\Generator\ClassNameGenerator.php:14 Stack trace: #0 C:\xampp\htdocs\migration\vendor\doctrine\migrations\lib\Doctrine\Migrations\Tools\Console\Command\GenerateCommand.php(67): Doctrine\Migrations\Generator\ClassNameGenerator->generateClassName(NULL) #1 C:\xampp\htdocs\migration\vendor\symfony\console\Command\Command.php(299): Doctrine\Migrations\Tools\Console\Command\GenerateCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #2 C:\xampp\htdocs\migration\vendor\symfony\console\Application.php(978): Symfony\Component\Console\Command\Command->run(Object(Symfony\Com in C:\xampp\htdocs\migration\vendor\doctrine\migrations\lib\Doctrine\Migrations\Generator\ClassNameGenerator.php on line 14
Minhas configurações:
- – PHP 7.4.13
- – Apache/2.4.46
- – MariaDB (10.4.17-MariaDB)
- – Tudo isso rodando no velho e bom xampp (Windows 10)
Meus pacotes no composer.json:
"require": { "php": "^7.3 || ~8.0.0", "laminas/laminas-component-installer": "^2.4", "laminas/laminas-development-mode": "^3.2", "laminas/laminas-skeleton-installer": "^0.3", "laminas/laminas-mvc": "^3.1.1", "laminas/laminas-cache": "^2.11.1", "laminas/laminas-mvc-form": "^1.1.0", "laminas/laminas-json": "^3.2", "laminas/laminas-cli": "^1.1.1", "laminas/laminas-mvc-plugins": "^1.1.0", "laminas/laminas-mvc-middleware": "^2.0.0", "laminas/laminas-session": "^2.10.0", "laminas/laminas-di": "^3.2.2", "doctrine/doctrine-orm-module": "^4.0", "doctrine/migrations": "^3.2" },
Me deparei com esse erro no mínimo esquisito ao tentar executar o generate do doctrine migrations, pois até então havia trabalhado com ele no passado e não necessitava nada mais para rodar com sucesso, pois bem, para solucionar fiz inúmeras pesquisas e pouco encontrei, mas consegui encontrar a solução e vou disponibilizar aqui.
Em alguns fóruns até visualizei algumas pessoas falando a respeito do zend-code ou laminas-code que não estaria sendo carregado por não reconhecer o namespace (por conta da mudança dos pacotes de zend para laminas), mas não constatei verídico.
Foi então que embarquei nas pesquisas para ver o que mudou, depois de dar uma varrida na documentação encontrei a solução, faltava especificar as configurações do migrations (Aparentemente agora é obrigatório).
Acesse seu arquivo de configuração de conexão com o banco, no meu caso como exemplo:
config/autoload/local.php
<?php return array( 'doctrine' => array( 'connection' => array( // default connection name 'orm_default' => array( 'driverClass' => Doctrine\DBAL\Driver\PDOMySql\Driver::class, 'params' => array( 'host' => '127.0.0.1', 'port' => '3306', 'user' => 'root', 'password' => '', 'dbname' => 'migration', 'driverOptions' => array( PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES 'UTF8'", ), ), ), ), // ******** ADICIONE ESSAS CONFIGURAÇÕES ******** 'migrations_configuration' => [ 'orm_default' => [ 'table_storage' => [ 'table_name' => 'DoctrineMigrationVersions', 'version_column_name' => 'version', 'version_column_length' => 1024, 'executed_at_column_name' => 'executedAt', 'execution_time_column_name' => 'executionTime', ], // ABAIXO VOCÊ PODE IR INSERINDO SEUS MÓDULOS 'migrations_paths' => [ 'Application'=> 'data/DoctrineORMModule/Migrations' ], // an array of namespace => path 'migrations' => [], // an array of fully qualified migrations 'all_or_nothing' => false, 'check_database_platform' => true, 'organize_migrations' => 'year_and_month', // year or year_and_month 'custom_template' => null, ], 'orm_other' => [ ] ], ), );
Caso o diretório “data/DoctrineORMModule/Migrations” não exista, então crie ele.
Depois de inserir as configurações basta tentar executar o comando novamente:
vendor\bin\doctrine-module migrations:generate
A reposta deve ser:
Generated new migration class to "data/DoctrineORMModule/Migrations\2021\09/Version20210930023441.php" To run just this migration for testing purposes, you can use migrations:execute --up 'Application\Version20210930023441' To revert the migration you can use migrations:execute --down 'Application\Version20210930023441'
Fontes:
https://www.doctrine-project.org/projects/doctrine-orm-module/en/4.0.x/migrations.html
https://www.doctrine-project.org/projects/doctrine-migrations/en/3.2/reference/configuration.html
https://stackoverflow.com/questions/21499253/doctrine-migrations-not-using-specified-configuration
https://olegkrivtsov.github.io/using-zend-framework-3-book/html/en/Database_Migrations/Configuring_Migrations.html
https://stackoverflow.com/questions/66508800/how-can-i-configure-doctrine-with-migrations
Links Úteis:
https://github.com/doctrine/DoctrineORMModule
https://www.doctrine-project.org/projects/doctrine-migrations/en/3.2/reference/configuration.html