Symfony2 implements the built-in data method in doctrine, symfony2doctrine_PHP tutorial

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.