《PHP學(xué)習(xí):Symfony2實現(xiàn)在doctrine中內(nèi)置數(shù)據(jù)的方法》要點(diǎn):
本文介紹了PHP學(xué)習(xí):Symfony2實現(xiàn)在doctrine中內(nèi)置數(shù)據(jù)的方法,希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP實戰(zhàn)本文實例講述了Symfony2實現(xiàn)在doctrine中內(nèi)置數(shù)據(jù)的辦法.分享給大家供大家參考,具體如下:
PHP實戰(zhàn)我們在使用symfony的時候,有時需要在數(shù)據(jù)庫中內(nèi)置一些數(shù)據(jù),那么我們?nèi)绨苍赿octrine中設(shè)置呢?
PHP實戰(zhàn)所幸,symfony已經(jīng)為我們封裝好了.這里,我們需要用到DoctrineFixturesBundle.
PHP實戰(zhàn)第一步,在composer.json中引入所需的DoctrineFixturesBundle:
PHP實戰(zhàn)
{
"require": {
"doctrine/doctrine-fixtures-bundle": "2.2.*"
}
}
PHP實戰(zhàn)第二步,執(zhí)行composer:
PHP實戰(zhàn)
composer update doctrine/doctrine-fixtures-bundle
PHP實戰(zhàn)第三步,在內(nèi)核(app/AppKernel.php)中注冊此bundle:
PHP實戰(zhàn)
// ...
public function registerBundles()
{
$bundles = array(
// ...
new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
// ...
);
// ...
}
PHP實戰(zhàn)第四步,在需要內(nèi)置數(shù)據(jù)的bundle下創(chuàng)建一個PHP類文件,如src/Acme/HelloBundle/DataFixtures/ORM/LoadUserData.php,其代碼如下:
PHP實戰(zhàn)
// 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();
}
}
PHP實戰(zhàn)第五步,通過console執(zhí)行內(nèi)置數(shù)據(jù)命令:
PHP實戰(zhàn)
php app/console doctrine:fixtures:load #為防止數(shù)據(jù)庫中原先的值被清除,可使用 --append 參數(shù)
PHP實戰(zhàn)此命令有以下三個參數(shù):
PHP實戰(zhàn)Cfixtures=/path/to/fixture C Use this option to manually specify the directory where the fixtures classes should be loaded;
Cappend C Use this flag to append data instead of deleting data before loading it (deleting first is the default behavior);
Cem=manager_name C Manually specify the entity manager to use for loading the data.
PHP實戰(zhàn)官方文檔:http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html
PHP實戰(zhàn)本文永久地址:http://blog.it985.com/6662.html
本文出自 IT985博客 ,轉(zhuǎn)載時請注明出處及相應(yīng)鏈接.
PHP實戰(zhàn)更多關(guān)于PHP框架相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php優(yōu)秀開發(fā)框架總結(jié)》,《codeigniter入門教程》,《CI(CodeIgniter)框架進(jìn)階教程》,《Yii框架入門及常用技巧總結(jié)》及《ThinkPHP入門教程》
PHP實戰(zhàn)希望本文所述對大家基于Symfony框架的PHP程序設(shè)計有所贊助.
維易PHP培訓(xùn)學(xué)院每天發(fā)布《PHP學(xué)習(xí):Symfony2實現(xiàn)在doctrine中內(nèi)置數(shù)據(jù)的方法》等實戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/7577.html