Is it possible to load doctrine fixtures without deleting database
Use the --append
option
php app/console help doctrine:fixtures:loadUsage: 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
DoctrineFixtures are nice for the first init of an empty database or in development - but not in production.
Take a look at DoctrineMigrations and symfonys DcotrineMigrationsBundle, this is a good and safe way to migrate your database in production.
It is possible to update previously added data to DB (which was loaded by running app/console doctrine:fixtures:load). I used EntityManager->createQuery for that.
But I still wonder if there is or ever will be an opportunity to do it by simply running an app/console command. Something like app/console doctrine:schema:update --force, but applied to the data itself.
For now I had to write a ton of code to get my data updated and appended properly. For example to make --append work I had to write the following:
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(); } } }