0

我遵循这本书的快车道,第 8.7 章 Migrer la base de données。

我通过以下方式正确生成了我的 2 个实体类symfony console make:entity

ls -1 src/Entity/Co*
src/Entity/Comment.php
src/Entity/Conference.php

https://pastebin.com/6RJmQTEg
https://pastebin.com/XwZ7csS3

symfony console make:migration,只得到:


<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
final class Version20200322201522 extends AbstractMigration
{
    public function getDescription() : string
    {
        return '';
    }

    public function up(Schema $schema) : void
    {
        // this up() migration is auto-generated, please modify it to your needs

    }

    public function down(Schema $schema) : void
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');

        $this->addSql('CREATE SCHEMA public');
    }
}

为什么没有生成 SQL 代码?

我的.env

APP_ENV=dev
APP_SECRET=xxx
DATABASE_URL=postgresql://127.0.0.1:5432/db?serverVersion=11&charset=utf8`

命令symfony run psql运行良好;我得到 psql 提示:

[mevatlave:~/sources/guestbook] master* ± symfony run psql
psql (11.7 (Debian 11.7-0+deb10u1))
Type "help" for help.

main=# 
symfony -V
Symfony CLI version v4.13.3 (Thu Mar 19 15:24:08 UTC 2020)
php bin/console debug:container --env-vars

Symfony Container Environment Variables
=======================================

 -------------- --------------- ---------------------------------------------------------------- 
  Name           Default value   Real value                                                      
 -------------- --------------- ---------------------------------------------------------------- 
  APP_SECRET     n/a             "4ec3efda62f1b3001e78459335893c3f"                              
  DATABASE_URL   n/a             "postgresql://127.0.0.1:5432/db?serverVersion=11&charset=utf8"  
 -------------- --------------- ---------------------------------------------------------------- 

 // Note real values might be different between web and CLI. 

从未编辑过 Symfony 配置:

cat config/packages/doctrine.yaml
doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'

        # IMPORTANT: You MUST configure your server version,
        # either here or in the DATABASE_URL env var (see .env file)
        #server_version: '5.7'
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App
4

2 回答 2

0

OK,发现书上的一个bug,DSN不对:

DATABASE_URL=postgresql://127.0.0.1:5432/db?serverVersion=11&charset=utf8

现在 :DATABASE_URL=postgresql://main:main@127.0.0.1:5432/db?serverVersion=11&charset=utf8

缺少用户名/密码。现在 symfonyconsole make:migration运行良好

于 2020-03-24T17:39:34.747 回答
0

尝试先清除缓存。

否则试试这个:你需要教义:迁移:差异。这将生成一个新的迁移,其中预先填充了您从教义:架构:更新中获得的 SQL

所以:

bin/console doctrine:migrations:diff
OR
bin/console d:m:d

然后你去检查新创建的文件,如果一切正常,你执行它:

// (to be 100% sure take a backup of your database before doing:)

bin/console doctrine:migrations:migrate
OR
bin/console d:m:m

如果您因为犯了错误或其他原因而想回去

// Replace 20190215095613 with your migration number!
bin/console doctrine:migrations:execute --down 20190215095613
  • 然后您可以修改迁移并再次执行“bin/console d:m:m”
  • 或者放弃迁移并从“bin/console d:m:d”重新开始
于 2020-03-24T15:56:34.337 回答