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
php日历[测试通过]
Mar 27 PHP
PHP伪造referer实例代码
Sep 20 PHP
php 魔术方法使用说明
Oct 20 PHP
PHP统计二维数组元素个数的方法
Nov 12 PHP
php不写闭合标签的好处
Mar 04 PHP
改写ThinkPHP的U方法使其路由下分页正常
Jul 02 PHP
CodeIgniter安全相关设置汇总
Jul 03 PHP
php格式化日期实例分析
Nov 12 PHP
分享ThinkPHP3.2中关联查询解决思路
Sep 20 PHP
PHP的Laravel框架中使用消息队列queue及异步队列的方法
Mar 21 PHP
详解Yii2 定制表单输入字段的标签和样式
Jan 04 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
discuz authcode 经典php加密解密函数解析
2020/07/12 PHP
DISCUZ在win2003环境下 Unable to access ./include/common.inc.php in... 的问题终极解决方案
2011/11/21 PHP
PHP图片裁剪与缩放示例(无损裁剪图片)
2017/02/08 PHP
laravel 5.1下php artisan migrate的使用注意事项总结
2017/06/07 PHP
PHP实现求两个字符串最长公共子串的方法示例
2017/11/17 PHP
JavaScript建立一个语法高亮输入框实现思路
2013/02/26 Javascript
JS中prototype的用法实例分析
2015/03/19 Javascript
jQuery使用hide方法隐藏元素自身用法实例
2015/03/30 Javascript
javascript实现了照片拖拽点击置顶的照片墙代码
2015/04/03 Javascript
jQuery里filter()函数与find()函数用法分析
2015/06/24 Javascript
js实现点击向下展开的下拉菜单效果代码
2015/09/01 Javascript
JavaScript知识点总结(六)之JavaScript判断变量数据类型
2016/05/31 Javascript
jQuery根据ID、CLASS、等获取对象的实例
2016/12/04 Javascript
JavaScript利用fetch实现异步请求的方法实例
2017/07/26 Javascript
在vue中多次调用同一个定义全局变量的实例
2018/09/25 Javascript
JS判断用户用的哪个浏览器实例详解
2018/10/09 Javascript
JavaScript实现抖音罗盘时钟
2019/10/11 Javascript
JS数据类型(基本数据类型、引用数据类型)及堆和栈的区别分析
2020/03/04 Javascript
javascript中可能用得到的全部的排序算法
2020/03/05 Javascript
js实现简单商品筛选功能
2021/02/02 Javascript
pymongo为mongodb数据库添加索引的方法
2015/05/11 Python
Java Web开发过程中登陆模块的验证码的实现方式总结
2016/05/25 Python
代码详解django中数据库设置
2019/01/28 Python
python3多线程知识点总结
2019/09/26 Python
python实现简单图书管理系统
2019/11/22 Python
Matplotlib配色之Colormap详解
2021/01/05 Python
HTML5几个设计和修改的页面范例分享
2015/09/29 HTML / CSS
大韩航空官方网站:Korean Air
2017/10/25 全球购物
LUISAVIAROMA中国官网:时尚奢侈品牌购物网站
2020/11/01 全球购物
生产部厂长助理职位说明书
2014/03/03 职场文书
2014年五四青年节活动方案
2014/03/29 职场文书
酒店员工培训方案
2014/06/02 职场文书
高考励志标语
2014/06/05 职场文书
第一批党的群众路线教育实践活动总结报告
2014/07/03 职场文书
小学大队长竞选稿
2015/11/20 职场文书
《普罗米修斯》教学反思
2016/02/22 职场文书