加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 03:07:38 所属栏目:PHP教程 来源:网络整理
导读:《:Symfony2实现在doctrine中内置数据的方法》要点: 本文介绍了:Symfony2实现在doctrine中内置数据的方法,希望对您有用。如果有疑问,可以联系我们。 PHP实战 本篇章节讲解Symfony2实现在doctrine中内置数据的办法.供大家参考研究,具体如下: PH

《:Symfony2实现在doctrine中内置数据的方法》要点:
本文介绍了:Symfony2实现在doctrine中内置数据的方法,希望对您有用。如果有疑问,可以联系我们。

PHP实战本篇章节讲解Symfony2实现在doctrine中内置数据的办法.分享给大家供大家参考,具体如下:

PHP实战我们在使用symfony的时候,有时需要在数据库中内置一些数据,那么我们如安在doctrine中设置呢?

PHP实战所幸,symfony已经为我们封装好了.这里,我们需要用到DoctrineFixturesBundle.

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

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

PHP实战第二步,执行composer:

PHP实战
composer update doctrine/doctrine-fixtures-bundle

PHP实战第三步,在内核(app/AppKernel.php)中注册此bundle:

PHP实战
// ...
public function registerBundles()
{
  $bundles = array(
    // ...
    new DoctrineBundleFixturesBundleDoctrineFixturesBundle(),// ...
  );
  // ...
}

PHP实战第四步,在需要内置数据的bundle下创建一个PHP类文件,如src/Acme/HelloBundle/DataFixtures/ORM/LoadUserData.php,其代码如下:

PHP实战
// src/Acme/HelloBundle/DataFixtures/ORM/LoadUserData.php
namespace AcmeHelloBundleDataFixturesORM;
use DoctrineCommonDataFixturesFixtureInterface;
use DoctrineCommonPersistenceObjectManager;
use AcmeHelloBundleEntityUser;
class LoadUserData implements FixtureInterface
{
  /**
   * {@inheritDoc}
   */
  public function load(ObjectManager $manager)
  {
    $userAdmin = new User();
    $userAdmin->setUsername('admin');
    $userAdmin->setPassword('test');
    $manager->persist($userAdmin);
    $manager->flush();
  }
}

PHP实战第五步,通过console执行内置数据命令:

PHP实战
php app/console doctrine:fixtures:load #为防止数据库中原先的值被清除,可使用 --append 参数

PHP实战此命令有以下三个参数:

PHP实战Cfixtures=/path/to/fixture C Use this option to manually specify the directory where the fixtures classes should be loaded;
Cappend C Use this flag to append data instead of deleting data before loading it (deleting first is the default behavior);
Cem=manager_name C Manually specify the entity manager to use for loading the data.

PHP实战官方文档:http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html

PHP实战本文永久地址:http://blog.it985.com/6662.html
本文出自 IT985博客,转载时请注明出处及相应链接.

PHP实战更多关于PHP框架相关内容感兴趣的读者可查看本站专题:《php优秀开发框架总结》,《codeigniter入门教程》,《CI(CodeIgniter)框架进阶教程》,《Yii框架入门及常用技巧总结》及《ThinkPHP入门教程》

PHP实战希望本文所述对大家基于Symfony框架的PHP程序设计有所赞助.

编程之家培训学院每天发布《:Symfony2实现在doctrine中内置数据的方法》等实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读