【问题标题】:How to create a table from entity in Doctrine 2如何从 Doctrine 2 中的实体创建表
【发布时间】:2019-09-02 10:11:21
【问题描述】:

我在 SO 看到类似的问题,但它们已经过时且不再实际。所以,我想要的是使用模型定义在数据库中创建表。所以,我有一个看起来像这样的模型:

<?php

namespace Tests\Integration\Models;

/**
* @Entity
* @Table(name="test_model")
*/
class TestModel
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue
     */
    protected $id;

    /** @Column(type="integer") */
    protected $a;


    public function getId()
    {
        return $this->id;
    }

    public function getA()
    {
        return $this->a;
    }

    public function setA($a)
    {
        $this->a = $a;
    }
}

在我的单元测试中,我有一个工作代码,如下所示:

    <?php

namespace Tests\Integration;

use PHPUnit\Framework\TestCase;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Setup;
use Tests\Integration\Models\TestModel;
use Tests\Integration\Models\TestModelRepository;


final class IntegrationOrmTest extends TestCase
{
    protected static $em;

    protected static $er;

    public static function setUpBeforeClass()
    {
        $params = [
            'driver' => getenv("TEST_DB_DRIVER"),
            'host' => getenv("TEST_DB_HOST"),
            'dbname' => getenv("TEST_DB_NAME"),
            'user' => getenv("TEST_DB_USER"),
            'password' => getenv("TEST_DB_PASSWORD"),
            'port' => getenv("TEST_DB_PORT"),
        ];

        $config = Setup::createAnnotationMetadataConfiguration(["./Models"], true);
        self::$em = EntityManager::create($params, $config);
        self::$er = new TestModelRepository(self::$em);           
    }

    public function testEquals()
    {
        //works perfectly fine
        $data = new TestModel();
        $data->setA(123);
        self::$er->save($data);
        self::$em->flush();

        $this->assertTrue(true);
    }
}

此代码有效,只是因为在我的数据库中已经有一个表test_model。但我想在我的测试服中动态创建它并在所有测试后销毁它。我怎样才能以编程方式做到这一点?

【问题讨论】:

  • 在本地 sqlite db 中使用测试环境?
  • @Andrea Manzi。我将它与 postgresql 数据库一起使用
  • 你想只创建和删除 test_model 吗?您的项目中有多少个实体?
  • 那么您实际尝试了哪些相关代码?应该只是创建一个 SchemaTool 实例然后执行 drop/create schema 的问题。
  • @Cerad。我不知道要尝试什么代码。我尝试了 Doctrine DBAL 并且它有效,但现在我需要对 ORM 实体做同样的事情。

标签: php unit-testing doctrine-orm


【解决方案1】:

Doctrine 文档详细描述了schema generation

这是一个简单的例子:

final class IntegrationOrmTest extends TestCase
{
    public static function setUpBeforeClass()
    {
        $params = [
            'driver' => "pdo_mysql",
            'host' => "localhost",
            'dbname' => "s43x",
            'user' => "impd",
            'password' => "JalenHurts",
            //'port' => getenv("TEST_DB_PORT"),
        ];

        $config = Setup::createAnnotationMetadataConfiguration(["./Models"], true);
        self::$em = $em = EntityManager::create($params, $config);

        $tool = new \Doctrine\ORM\Tools\SchemaTool($em);
        $classes = array(
            $em->getClassMetadata(TestModel::class),
        );
        $tool->dropSchema($classes);  // Empty out any current schema
        $tool->createSchema($classes);

【讨论】:

  • 我更新了答案以显示 $config。基本上和你的一样,虽然我没有使用 getenv。
猜你喜欢
  • 1970-01-01
  • 2015-06-14
  • 2023-03-27
  • 1970-01-01
  • 2011-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多