Symfony2 a method to implement the data built in doctrine _php instance

Source: Internet
Author: User
Tags php class php framework codeigniter

The example in this article describes the Symfony2 method for implementing data built into the doctrine. Share to everyone for your reference, specific as follows:

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

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

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

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

Step Two, execute 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 (),
    //...
  );
  // ...
}

The fourth step is to create a PHP class file, such as src/acme/hellobundle/datafixtures/orm/loaduserdata.php, under the bundle that requires the built-in data, and its code is as follows:

src/acme/hellobundle/datafixtures/orm/loaduserdata.php
namespace 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 fifth, execute the built-in data command through the console:

PHP app/console doctrine:fixtures:load #为防止数据库中原先的值被清除, you can use--append parameters

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

This article permanent address: http://blog.it985.com/6662.html
This article comes from IT985 blog, reprint, please indicate the source and corresponding link.

For more information about PHP framework interested readers can view the site topics: "PHP Excellent Development Framework Summary", "CodeIgniter Introductory Course", "CI (CodeIgniter) Framework Advanced Course", "Yii framework Introduction and common skills Summary" and " thinkphp Getting Started Tutorial "

I hope this article will help you with the PHP program design based on Symfony framework.

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.