Symfony2 implements the built-in data method in doctrine, symfony2doctrine. Symfony2 implements the built-in data method in doctrine. symfony2doctrine describes how Symfony2 implements the built-in data method in doctrine. For your reference, see Symfony2's built-in data method in doctrine, symfony2doctrine.
This example describes how to implement built-in data in doctrine in Symfony2. We will share this with you for your reference. The details are as follows:
When using symfony, we sometimes need to build some data in the database. how can we set it in doctrine?
Fortunately, symfony has been encapsulated for us. Here, we need DoctrineFixturesBundle.
Step 1: Introduce the required DoctrineFixturesBundle in composer. json:
{ "require": { "doctrine/doctrine-fixtures-bundle": "2.2.*" }}
Step 2: execute composer:
composer update doctrine/doctrine-fixtures-bundle
Step 3: register this bundle in the kernel (app/AppKernel. php:
// ...public function registerBundles(){ $bundles = array( // ... new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(), // ... ); // ...}
Step 4: Create a PHP class file under the bundle that requires built-in data, such as src/Acme/HelloBundle/DataFixtures/ORM/LoadUserData. php. the code is as follows:
// src/Acme/HelloBundle/DataFixtures/ORM/LoadUserData.phpnamespace Acme\HelloBundle\DataFixtures\ORM;use Doctrine\Common\DataFixtures\FixtureInterface;use Doctrine\Common\Persistence\ObjectManager;use Acme\HelloBundle\Entity\User;class LoadUserData implements FixtureInterface{ /** * {@inheritDoc} */ public function load(ObjectManager $manager) { $userAdmin = new User(); $userAdmin->setUsername('admin'); $userAdmin->setPassword('test'); $manager->persist($userAdmin); $manager->flush(); }}
Step 5: run the built-in data command on the console:
Php app/console doctrine: fixtures: load # to prevent the first value in the database from being cleared, you can use the -- append parameter.
This command has the following three parameters:
-Fixtures =/path/to/fixture-Use this option to manually specify the directory where the fixtures classes shoshould be loaded;
-Append-Use this flag to append data instead of deleting data before loading it (deleting first is the default behavior );
-Em = manager_name-Manually specify the entity manager to use for loading the data.
Official documents: http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html
Permanent Address: http://blog.it985.com/6662.html
This article is from the IT985 blog. please indicate the source and relevant links when reprinting.