最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Symfony2實(shí)現(xiàn)在doctrine中內(nèi)置數(shù)據(jù)的方法

 更新時(shí)間:2016年02月05日 09:41:41   作者:Terry  
這篇文章主要介紹了Symfony2實(shí)現(xiàn)在doctrine中內(nèi)置數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了在doctrine中內(nèi)置數(shù)據(jù)的具體步驟與相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了Symfony2實(shí)現(xiàn)在doctrine中內(nèi)置數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:

我們在使用symfony的時(shí)候,有時(shí)需要在數(shù)據(jù)庫中內(nèi)置一些數(shù)據(jù),那么我們?nèi)绾卧赿octrine中設(shè)置呢?

所幸,symfony已經(jīng)為我們封裝好了。這里,我們需要用到DoctrineFixturesBundle。

第一步,在composer.json中引入所需的DoctrineFixturesBundle:

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

第二步,執(zhí)行composer:

composer update doctrine/doctrine-fixtures-bundle

第三步,在內(nèi)核(app/AppKernel.php)中注冊此bundle:

// ...
public function registerBundles()
{
  $bundles = array(
    // ...
    new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
    // ...
  );
  // ...
}

第四步,在需要內(nèi)置數(shù)據(jù)的bundle下創(chuàng)建一個(gè)PHP類文件,如src/Acme/HelloBundle/DataFixtures/ORM/LoadUserData.php,其代碼如下:

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

第五步,通過console執(zhí)行內(nèi)置數(shù)據(jù)命令:

php app/console doctrine:fixtures:load #為防止數(shù)據(jù)庫中原先的值被清除,可使用 --append 參數(shù)

此命令有以下三個(gè)參數(shù):

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 use for loading the data.

官方文檔:http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html

本文永久地址:http://blog.it985.com/6662.html
本文出自 IT985博客 ,轉(zhuǎn)載時(shí)請注明出處及相應(yīng)鏈接。

更多關(guān)于PHP框架相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php優(yōu)秀開發(fā)框架總結(jié)》,《codeigniter入門教程》,《CI(CodeIgniter)框架進(jìn)階教程》,《Yii框架入門及常用技巧總結(jié)》及《ThinkPHP入門教程

希望本文所述對大家基于Symfony框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論

克山县| 安平县| 武乡县| 枣强县| 玉溪市| 垫江县| 石狮市| 永春县| 宝兴县| 无极县| 肃宁县| 商水县| 淮滨县| 沽源县| 诸城市| 镇远县| 北海市| 衡山县| 从化市| 白朗县| 道孚县| 满洲里市| 定襄县| 普宁市| 丰原市| 田东县| 思南县| 宁夏| 高尔夫| 扎鲁特旗| 汝州市| 敦化市| 云梦县| 汉阴县| 潞西市| 蓬莱市| 德安县| 西安市| 鄂伦春自治旗| 德格县| 临安市|