国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

Symfony2實現在doctrine中內置數據的方法

2024-05-04 23:42:52
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了Symfony2實現在doctrine中內置數據的方法,結合實例形式分析了在doctrine中內置數據的具體步驟與相關技巧,需要的朋友可以參考下
 

本文實例講述了Symfony2實現在doctrine中內置數據的方法。分享給大家供大家參考,具體如下:

我們在使用symfony的時候,有時需要在數據庫中內置一些數據,那么我們如何在doctrine中設置呢?

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

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

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

第二步,執行composer:

composer update doctrine/doctrine-fixtures-bundle

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

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

第四步,在需要內置數據的bundle下創建一個PHP類文件,如src/Acme/HelloBundle/DataFixtures/ORM/LoadUserData.php,其代碼如下:

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

第五步,通過console執行內置數據命令:

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

此命令有以下三個參數:

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博客 ,轉載時請注明出處及相應鏈接。



注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 望江县| 治多县| 仪陇县| 库尔勒市| 济源市| 长寿区| 广元市| 涞水县| 张北县| 兰州市| 茶陵县| 博野县| 北川| 普兰县| 昌都县| 思南县| 太康县| 滕州市| 宁安市| 景东| 偃师市| 玛纳斯县| 阜阳市| 泸西县| 新竹县| 富裕县| 宝丰县| 广德县| 桑日县| 类乌齐县| 中江县| 石渠县| 诸城市| 宜兴市| 枣强县| 金昌市| 和田县| 新沂市| 都江堰市| 吕梁市| 桦南县|