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中var_dump,var_export,print_r三个函数的区别
Jun 21 PHP
php的memcache类分享(memcache队列)
Mar 26 PHP
php查找指定目录下指定大小文件的方法
Nov 28 PHP
PHP数组和explode函数示例总结
May 08 PHP
win10环境PHP 7 安装配置【教程】
May 09 PHP
Linux php 中文乱码的快速解决方法
May 13 PHP
利用Homestead快速运行一个Laravel项目的方法详解
Nov 14 PHP
PHP多线程模拟实现秒杀抢单
Feb 07 PHP
php记录搜索引擎爬行记录的实现代码
Mar 02 PHP
php微信公众号开发之欢迎老朋友
Oct 20 PHP
Yii2框架配置文件(Application属性)与调试技巧实例分析
May 27 PHP
laravel 如何实现引入自己的函数或类库
Oct 15 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
使用php4加速网络传输
2006/10/09 PHP
PHP生成RSS文件类实例
2014/12/05 PHP
在WordPress的文章编辑器中设置默认内容的方法
2015/12/29 PHP
php数组分页实现方法
2016/04/30 PHP
php基于环形链表解决约瑟夫环问题示例
2017/11/07 PHP
thinkPHP框架RBAC实现原理分析
2019/02/01 PHP
在html页面上拖放移动标签
2010/01/08 Javascript
jQuery.each()用法分享
2012/07/31 Javascript
js/jquery去掉空格,回车,换行示例代码
2013/11/05 Javascript
移动设备web开发首选框架:zeptojs介绍
2015/01/29 Javascript
JavaScript中setFullYear()方法的使用详解
2015/06/11 Javascript
JS实现随机颜色的3种方法与颜色格式的转化
2017/01/05 Javascript
详解Angular 4 表单快速入门
2017/06/05 Javascript
node中Express 动态设置端口的方法
2017/08/04 Javascript
3种vue路由传参的基本模式
2018/02/22 Javascript
angular5 httpclient的示例实战
2018/03/12 Javascript
JS限制输入框输入的实现代码
2018/07/02 Javascript
微信小程序websocket实现聊天功能
2020/03/30 Javascript
不要用强制方法杀掉python线程
2017/02/26 Python
python实现数据图表
2017/07/29 Python
使用Scrapy爬取动态数据
2018/10/21 Python
pandas的连接函数concat()函数的具体使用方法
2019/07/09 Python
Python+AutoIt实现界面工具开发过程详解
2019/08/07 Python
Python 3.8 新功能大揭秘【新手必学】
2020/02/05 Python
解决django 向mysql中写入中文字符出错的问题
2020/05/18 Python
keras中的卷积层&池化层的用法
2020/05/22 Python
详解python内置模块urllib
2020/09/09 Python
Python暴力破解Mysql数据的示例
2020/11/09 Python
html5理解head_动力节点Java学院整理
2017/07/13 HTML / CSS
HTML5引入的新数组TypedArray介绍
2012/12/24 HTML / CSS
杭州信雅达系统.NET工程师面试试题
2015/02/08 面试题
30年同学聚会感言
2014/01/30 职场文书
竞选体育委员演讲稿
2014/04/26 职场文书
求职信怎么写
2014/05/23 职场文书
党委工作总结2015
2015/04/27 职场文书
浅谈Python数学建模之数据导入
2021/06/23 Python