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.