【问题标题】:Is it possible to load doctrine fixtures without deleting database是否可以在不删除数据库的情况下加载学说夹具
【发布时间】:2014-04-06 21:37:55
【问题描述】:

我在doctrine中定义了一些fixtures

当我尝试使用它运行时

php app/console doctrine:fixtures:load 然后它要求我清除数据库。

是否可以在不清除数据库的情况下加载它。

我记得 Djangofixtures 可以在不清除现有数据库的情况下加载到单独的表中

【问题讨论】:

    标签: php symfony doctrine-orm fixtures


    【解决方案1】:

    DoctrineFixtures 非常适合空数据库的第一次初始化或正在开发中 - 但不适用于生产环境。

    看看 DoctrineMigrations 和 symfonys DcotrineMigrationsBundle,这是在生产环境中迁移数据库的好方法。

    【讨论】:

      【解决方案2】:

      可以将之前添加的数据更新到 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();
                  }
              }
          }
      

      【讨论】:

      • 还有一个问题。将数据传递给 FixtureInterface 实现的最佳方式是什么?我认为将它声明为一个数组(就像我上面的 sh*tcoded 一样)不是有史以来最有效的方式.. Thnx.
      【解决方案3】:

      使用--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
      

      【讨论】:

      • 酷,谢谢。不幸的是,这再次添加了已经添加的条目。因此,它会导致重复。有没有可能避免这种情况?
      • @Simon 是的,不要再次加载它们。将您的新灯具放在一个单独的文件夹中(日期戳是个好名字)并使用--fixtures 选项。
      • 没有其他可能吗?遗憾。然后我必须知道自己已经加载了哪些灯具以及我还需要加载哪些灯具。不是一个真正可行的解决方案......
      • 我为DoctrineFixturesBundle GitHub repository中的问题创建了一个问题。
      • @Phil 我们如何排除某些要通过doctrine:fixtures:load 清除的表(例如,国家、城市等)?有没有yaml配置文件之类的?
      猜你喜欢
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      相关资源
      最近更新 更多