Symfony2 Implementing a method to build data in doctrine, symfony2doctrine_php tutorial

Source: Internet
Author: User
Tags php framework codeigniter

Symfony2 implements the method of data built into the doctrine, Symfony2doctrine


The example in this paper describes how Symfony2 implements the data built into the doctrine. Share to everyone for your reference, as follows:

When we use symfony, sometimes we need to have some data built into the database, so how do we set it up in doctrine?

Fortunately, Symfony has been packaged for us. Here, we need to use the doctrinefixturesbundle.

The first step is to introduce the required doctrinefixturesbundle in the Composer.json:

{"  require": {    "doctrine/doctrine-fixtures-bundle": "2.2.*"  }}

The second step is to execute the composer:

Composer Update Doctrine/doctrine-fixtures-bundle

The third step is to register this bundle in the kernel (app/appkernel.php):

... public function registerbundles () {  $bundles = array (    //...    ) New Doctrine\bundle\fixturesbundle\doctrinefixturesbundle (),    //...  );  // ...}

Fourth step, create a PHP class file under the bundle that requires the built-in data, such as src/acme/hellobundle/datafixtures/orm/loaduserdata.php, with the following code:

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 ();  }}

The fifth step is to execute the built-in data command via the console:

PHP app/console doctrine:fixtures:load #为防止数据库中原先的值被清除, 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 should 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 loading the data.

Official Document: Http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html

Permanent address of this article: http://blog.it985.com/6662.html
This article is from IT985 blog, please indicate the source and corresponding link when reproduced.

Read more about PHP framework related content Readers can view this site: "PHP Excellent Development Framework Summary", "CodeIgniter Introductory Tutorial", "CI (codeigniter) Framework Advanced Tutorial", "Yii framework Introduction and common skills Summary" and " thinkphp Getting Started Tutorial "

It is hoped that this article is helpful to the PHP program design based on Symfony framework.

Articles you may be interested in:

    • Symfony2 installing a third-party bundles instance
    • Symfony2 use third-party library upload to make picture upload instance detailed
    • The configuration method of Symfony2 under Nginx and graphic tutorial
    • Symfony2 Installation Method (2 methods)
    • Symfony2 Session Usage Example analysis
    • High performance PHP Framework Symfony2 Classic Introductory Tutorial
    • Symfony Learning 10-minute Introductory classic tutorial
    • An example analysis of Symfony data verification method
    • Symfony form and page implementation tips
    • An example analysis of controller usage of SYMFONY2 development

http://www.bkjia.com/PHPjc/1099080.html www.bkjia.com true http://www.bkjia.com/PHPjc/1099080.html techarticle Symfony2 implements the method of data built in doctrine, Symfony2doctrine This article describes the SYMFONY2 implementation of the data in the Doctrine method. Share to everyone for your reference, specific as ...

  • Related Article

    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.