cakephp3のセットアップ中にめっちゃハマったので忘れないようにメモ
環境
- vagrant
- centos6
- nginx 1.14.1
- php 7.2.8
- cakephp 3.6
- mysql mariaDB 10.3.8
事象
migrationが動かない。
正確に言うと、migrationが上手く動かない。
composerでcakephp3を導入したけども、どうもダメっぽい。
[vagrant@local app]$ bin/cake migrations status
using migration paths
- /var/www/html/app/config/Migrations
using seed paths
- /var/www/html/app/config/Seeds
Notice Error: A non well formed numeric value encountered in [/var/www/html/app/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/AbstractAdapter.php, line 229]
2018-08-14 02:53:38 Notice: Notice (8): A non well formed numeric value encountered in [/var/www/html/app/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/AbstractAdapter.php, line 229]
Trace:
Cake\Error\BaseErrorHandler::handleError() - CORE/src/Error/BaseErrorHandler.php, line 159
Exception::__construct() - [internal], line ??
Phinx\Db\Adapter\AbstractAdapter::createSchemaTable() - ROOT/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/AbstractAdapter.php, line 229
Phinx\Db\Adapter\PdoAdapter::setConnection() - ROOT/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php, line 104
Phinx\Db\Adapter\MysqlAdapter::connect() - ROOT/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/MysqlAdapter.php, line 127
Phinx\Db\Adapter\PdoAdapter::getConnection() - ROOT/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php, line 134
Phinx\Db\Adapter\AdapterWrapper::getConnection() - ROOT/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/AdapterWrapper.php, line 461
Migrations\CakeAdapter::__construct() - ROOT/vendor/cakephp/migrations/src/CakeAdapter.php, line 56
Migrations\Command\Status::bootstrap() - ROOT/vendor/cakephp/migrations/src/Command/CommandTrait.php, line 78
Migrations\Command\Status::execute() - ROOT/vendor/cakephp/migrations/src/Command/Status.php, line 55
Symfony\Component\Console\Command\Command::run() - ROOT/vendor/symfony/console/Command/Command.php, line 251
Symfony\Component\Console\Application::doRunCommand() - ROOT/vendor/symfony/console/Application.php, line 886
Symfony\Component\Console\Application::doRun() - ROOT/vendor/symfony/console/Application.php, line 262
Symfony\Component\Console\Application::run() - ROOT/vendor/symfony/console/Application.php, line 145
Migrations\Shell\MigrationsShell::main() - ROOT/vendor/cakephp/migrations/src/Shell/MigrationsShell.php, line 108
Cake\Console\Shell::runCommand() - CORE/src/Console/Shell.php, line 532
Migrations\Shell\MigrationsShell::runCommand() - ROOT/vendor/cakephp/migrations/src/Shell/MigrationsShell.php, line 164
Cake\Console\CommandRunner::runShell() - CORE/src/Console/CommandRunner.php, line 342
Cake\Console\CommandRunner::run() - CORE/src/Console/CommandRunner.php, line 164
[main] - ROOT/bin/cake.php, line 12
Exception: There was a problem creating the schema table: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in [/var/www/html/app/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/AbstractAdapter.php, line 226]
2018-08-14 02:53:38 Error: [InvalidArgumentException] There was a problem creating the schema table: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in /var/www/html/app/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/AbstractAdapter.php on line 226
Stack Trace:
#0 /var/www/html/app/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php(104): Phinx\Db\Adapter\AbstractAdapter->createSchemaTable()
#1 /var/www/html/app/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/MysqlAdapter.php(127): Phinx\Db\Adapter\PdoAdapter->setConnection(Object(PDO))
#2 /var/www/html/app/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php(134): Phinx\Db\Adapter\MysqlAdapter->connect()
#3 /var/www/html/app/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/AdapterWrapper.php(461): Phinx\Db\Adapter\PdoAdapter->getConnection()
#4 /var/www/html/app/vendor/cakephp/migrations/src/CakeAdapter.php(56): Phinx\Db\Adapter\AdapterWrapper->getConnection()
#5 /var/www/html/app/vendor/cakephp/migrations/src/Command/CommandTrait.php(78): Migrations\CakeAdapter->__construct(Object(Phinx\Db\Adapter\TimedOutputAdapter), Object(Cake\Database\Connection))
#6 /var/www/html/app/vendor/cakephp/migrations/src/Command/Status.php(55): Migrations\Command\Status->bootstrap(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#7 /var/www/html/app/vendor/symfony/console/Command/Command.php(251): Migrations\Command\Status->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#8 /var/www/html/app/vendor/symfony/console/Application.php(886): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#9 /var/www/html/app/vendor/symfony/console/Application.php(262): Symfony\Component\Console\Application->doRunCommand(Object(Migrations\Command\Status), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#10 /var/www/html/app/vendor/symfony/console/Application.php(145): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#11 /var/www/html/app/vendor/cakephp/migrations/src/Shell/MigrationsShell.php(108): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#12 /var/www/html/app/vendor/cakephp/cakephp/src/Console/Shell.php(532): Migrations\Shell\MigrationsShell->main('migrations', 'status')
#13 /var/www/html/app/vendor/cakephp/migrations/src/Shell/MigrationsShell.php(164): Cake\Console\Shell->runCommand(Array, true, Array)
#14 /var/www/html/app/vendor/cakephp/cakephp/src/Console/CommandRunner.php(342): Migrations\Shell\MigrationsShell->runCommand(Array, true)
#15 /var/www/html/app/vendor/cakephp/cakephp/src/Console/CommandRunner.php(164): Cake\Console\CommandRunner->runShell(Object(Migrations\Shell\MigrationsShell), Array)
#16 /var/www/html/app/bin/cake.php(12): Cake\Console\CommandRunner->run(Array)
#17 {main}
どうやらDBの接続が上手く行ってない模様。
localhostにhttpで接続するとDB接続はOKってでるので、設定は問題なさそう。
でもDBに繋がらない。。。なんで。。。
調査
最初はmigrationの初期設定とかいるのかなーぐらいに思ってましたが、そんなのはドキュメントに記載がない。つまりいらないはず。
で、めっちゃggった。
cakephp3 migration maria
とか
cakephp3 migration no database
とか
もちろんエラー文でも検索した。
でも、どこにも同じ事象が載って無くて、諦めてプラグインの中身を見てみることに。
cakephpのmigrationはPhinxのプラグインのラッパーなのでPhinxを調べることに。(エラーもPhinxだったし)
エラー箇所にdebugを仕込んでさかのぼりながら調査してみたら、なんと!
Adapterオブジェクトの中身のDB設定が全部nullになってましたorz
原因
で、原因はというと.env
を読み込めてなかったんですね。
DBのusernameとか全部.env
に記載してapp.php
で読み込む形にしてたのが仇になった。。。(みんなapp.phpに直書きなの?)
解決
/**
* Uncomment block of code below if you want to use `.env` file during development.
* You should copy `config/.env.default to `config/.env` and set/modify the
* variables as required.
*/
if (!env('APP_NAME') && file_exists(CONFIG . '.env')) {
$dotenv = new \josegonzalez\Dotenv\Loader([CONFIG . '.env']);
$dotenv->parse()
->putenv()
->toEnv()
->toServer();
}
bootstrap.php
の47〜58行目辺りにenv設定を読み込むコードがあったので、コメントアウトを外しました。
なんで、defaultで読み込む設定になってないんだろう・・・