Php 是否可以在不删除数据库的情况下加载设备
我在Php 是否可以在不删除数据库的情况下加载设备,php,symfony,doctrine-orm,fixtures,Php,Symfony,Doctrine Orm,Fixtures,我在条令中定义了一些装置 当我试着用这个跑步的时候 php应用程序/控制台原则:fixtures:load然后它要求我清除数据库 是否可以在不清除数据库的情况下加载它 我记得Django有装置,可以在单独的表中加载,而无需清除现有数据库使用--append选项 php app/console help doctrine:fixtures:load Usage: doctrine:fixtures:load [--fixtures[="..."]] [--append] [--em="..."]
条令
中定义了一些装置
当我试着用这个跑步的时候
php应用程序/控制台原则:fixtures:load
然后它要求我清除数据库
是否可以在不清除数据库的情况下加载它
我记得Django有装置
,可以在单独的表中加载,而无需清除现有数据库使用--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
可以将以前添加的数据更新到数据库中(通过运行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();
}
}
}
DoctrineFixtures适合于空数据库的第一次初始化或开发中,但不适合于生产中
看看和symfonys,这是在生产环境中迁移数据库的一种安全的好方法。很酷,谢谢。不幸的是,这会再次添加已添加的条目。因此,它会导致重复。有可能避免吗?@Simon是的,不要再加载了。将您的新装置放在一个单独的文件夹中(日期戳代表好名字),并使用
--fixtures
选项。没有其他可能性吗?可怜。然后我必须知道我已经加载了哪些装置,哪些装置还需要加载。这不是一个切实可行的解决方案……我在中为这个问题创建了一个问题。@Phil我们如何通过原则:fixture:load
排除一些要清除的表(例如,国家、城市等)?是否有任何yaml配置文件或其他文件?还有一个问题。向FixtureInterface实现传递数据的最佳方式是什么?我认为声明它是一个数组(像上面的SH编码)不是最有效的方法。Thnx。