Symfony2实现在doctrine中内置数据的方法


Posted in PHP onFebruary 05, 2016

本文实例讲述了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.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执行内置数据命令:

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博客 ,转载时请注明出处及相应链接。

希望本文所述对大家基于Symfony框架的PHP程序设计有所帮助。

PHP 相关文章推荐
PHP中上传大体积文件时需要的设置
Oct 09 PHP
PHP6 mysql连接方式说明
Feb 09 PHP
用php的ob_start来生成静态页面的方法分析
Mar 09 PHP
php后台如何避免用户直接进入方法实例
Oct 15 PHP
php不使用插件导出excel的简单方法
Mar 04 PHP
PHP开发中常见的安全问题详解和解决方法(如Sql注入、CSRF、Xss、CC等)
Apr 21 PHP
ThinkPHP权限认证Auth实例详解
Jul 22 PHP
常见的四种POST 提交数据方式(小总结)
Oct 08 PHP
PHP中list()函数用法实例简析
Jan 08 PHP
php parse_str() 函数的定义和用法
May 23 PHP
laravel 解决groupBy时出现的错误 isn't in Group By问题
Oct 17 PHP
PDO实现学生管理系统
Mar 21 PHP
PHP MYSQL实现登陆和模糊查询两大功能
Feb 05 #PHP
twig模板获取全局变量的方法
Feb 05 #PHP
twig里使用js变量的方法
Feb 05 #PHP
Symfony2安装第三方Bundles实例详解
Feb 04 #PHP
Symfony2使用第三方库Upload制作图片上传实例详解
Feb 04 #PHP
Symfony2在Nginx下的配置方法图文教程
Feb 04 #PHP
Symfony2安装的方法(2种方法)
Feb 04 #PHP
You might like
PHP flock 文件锁详细介绍
2012/12/29 PHP
PHP中使用foreach()遍历二维数组的简单实例
2016/06/13 PHP
PHP基于DOMDocument解析和生成xml的方法分析
2017/07/17 PHP
Laravel框架使用Seeder实现自动填充数据功能
2018/06/13 PHP
新浪的图片新闻效果
2007/01/13 Javascript
msn上的tab功能Firefox对childNodes处理的一个BUG
2008/01/21 Javascript
js自定义事件代码说明
2011/01/31 Javascript
node.js+express制作网页计算器
2016/01/17 Javascript
Bootstrap+jfinal实现省市级联下拉菜单
2016/05/30 Javascript
vue.js初学入门教程(2)
2016/11/07 Javascript
Vue如何引入远程JS文件
2017/04/20 Javascript
vue.js实现价格格式化的方法
2017/05/23 Javascript
jquery实现一个全局计时器(商城可用)
2017/06/30 jQuery
protractor的安装与基本使用教程
2017/07/07 Javascript
基于JQuery的Ajax方法使用详解
2017/08/16 jQuery
信息滚动效果的实例讲解
2017/09/18 Javascript
jquery获取transform里的值实现方法
2017/12/12 jQuery
Angularjs中的$apply及优化使用详解
2018/07/02 Javascript
JavaScript之解构赋值的理解
2019/01/30 Javascript
Vue.js实现开发购物车功能的方法详解
2019/02/22 Javascript
JavaScript解析JSON数据示例
2019/07/16 Javascript
Vue+Element实现网页版个人简历系统(推荐)
2019/12/31 Javascript
Vue记住滚动条和实现下拉加载的完美方法
2020/07/31 Javascript
Vue封装全局过滤器Filters的步骤
2020/09/16 Javascript
深入Python解释器理解Python中的字节码
2015/04/01 Python
Python下的subprocess模块的入门指引
2015/04/16 Python
举例详解Python中yield生成器的用法
2015/08/05 Python
Python单向链表和双向链表原理与用法实例详解
2018/08/31 Python
python下载微信公众号相关文章
2019/02/26 Python
python re库的正则表达式入门学习教程
2019/03/08 Python
HTML5自定义mp3播放器源码
2020/01/06 HTML / CSS
UNIONBAY官网:美国青少年服装品牌
2019/03/26 全球购物
网吧管理制度范本
2015/08/05 职场文书
街道办残联2016年助残日活动总结
2016/04/01 职场文书
深入理解python多线程编程
2021/04/18 Python
Java实现给Word文件添加文字水印
2022/02/15 Java/Android