如何从 Doctrine 2 中的实体创建 table
How to create a table from entity in Doctrine 2
我在 SO 看到类似的问题,但它们已经过时并且不再实际。所以,我想要的是使用模型定义在数据库中创建 tables。所以,我有一个看起来像这样的模型:
<?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);
}
}
此代码有效,只是因为在我的数据库中已经有 table test_model
。但我想在我的测试服中动态创建它并在所有测试后销毁它。我怎样才能以编程方式做到这一点?
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);
我在 SO 看到类似的问题,但它们已经过时并且不再实际。所以,我想要的是使用模型定义在数据库中创建 tables。所以,我有一个看起来像这样的模型:
<?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);
}
}
此代码有效,只是因为在我的数据库中已经有 table test_model
。但我想在我的测试服中动态创建它并在所有测试后销毁它。我怎样才能以编程方式做到这一点?
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);