【发布时间】:2014-04-06 21:37:55
【问题描述】:
我在doctrine中定义了一些fixtures。
当我尝试使用它运行时
php app/console doctrine:fixtures:load 然后它要求我清除数据库。
是否可以在不清除数据库的情况下加载它。
我记得 Django 有 fixtures 可以在不清除现有数据库的情况下加载到单独的表中
【问题讨论】:
标签: php symfony doctrine-orm fixtures
我在doctrine中定义了一些fixtures。
当我尝试使用它运行时
php app/console doctrine:fixtures:load 然后它要求我清除数据库。
是否可以在不清除数据库的情况下加载它。
我记得 Django 有 fixtures 可以在不清除现有数据库的情况下加载到单独的表中
【问题讨论】:
标签: php symfony doctrine-orm fixtures
DoctrineFixtures 非常适合空数据库的第一次初始化或正在开发中 - 但不适用于生产环境。
看看 DoctrineMigrations 和 symfonys DcotrineMigrationsBundle,这是在生产环境中迁移数据库的好方法。
【讨论】:
可以将之前添加的数据更新到 DB(通过运行 app/console 学说:fixtures:load 加载)。我为此使用了 EntityManager->createQuery。
但我仍然想知道是否有机会或曾经有机会通过简单地运行应用程序/控制台命令来做到这一点。类似于 app/console 学说:schema:update --force,但应用于数据本身。
现在我必须编写大量代码才能正确更新和附加我的数据。例如,要使 --append 工作,我必须编写以下内容:
class LoadCategoryData implements FixtureInterface
{
/**
* {@inheritDoc}
*/
public function load(ObjectManager $em)
{
//a category has name and shortcut name (st)
$categories = array (
[0]=> array('name' => 'foo', 'st' = 'bar'),
...
);
foreach ($categories as $cat) {
$query = $em->createQuery(
"SELECT c
FROM MyBundle:Category c
WHERE c.st = ?1"
)->setParameter(1, $cat['st'])->getResult();
if (count($query) == 0) {
$c = new Category();
$c->setName($cat['name']);
$c->setSt($cat['st']);
$em->persist($c);
}
$em->flush();
}
}
}
【讨论】:
使用--append 选项
php app/console help doctrine:fixtures:load
Usage:
doctrine:fixtures:load [--fixtures[="..."]] [--append] [--em="..."] [--purge-with-truncate]
Options:
--fixtures The directory or file to load data fixtures from. (multiple values allowed)
--append Append the data fixtures instead of deleting all data from the database first.
--em The entity manager to use for this command.
--purge-with-truncate Purge data by using a database-level TRUNCATE statement
【讨论】:
--fixtures 选项。
doctrine:fixtures:load 清除的表(例如,国家、城市等)?有没有yaml配置文件之类的?