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; PHP实战官方文档:http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html PHP实战本文永久地址:http://blog.it985.com/6662.html PHP实战更多关于PHP框架相关内容感兴趣的读者可查看本站专题:《php优秀开发框架总结》,《codeigniter入门教程》,《CI(CodeIgniter)框架进阶教程》,《Yii框架入门及常用技巧总结》及《ThinkPHP入门教程》 PHP实战希望本文所述对大家基于Symfony框架的PHP程序设计有所赞助. 编程之家培训学院每天发布《:Symfony2实现在doctrine中内置数据的方法》等实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |