±¾ÎÄʵÀý½²ÊoÁËSymfony2ʵÏÖÔÚdoctrineÖÐÄÚÖÃÊý¾ÝµÄ½¨¡£*ÖÏi¸ø´o¼Ò¹(C)´o¼Ò²Î¿¼£¬¾ßÌaÈçÏ£º
ÎÒÃÇÔÚʹÓÃsymfonyµÄʱºo£¬ÓÐʱÐeÒªÔÚÊý¾Ý¿aÖÐÄÚÖÃÒ»Ð(C)Êý¾Ý£¬ÄÇôÎÒÃÇÈçºÎÔÚdoctrineÖÐÉeÖÃÄØ£¿
ËuÐÒ£¬symfonyÒѾΪÎÒÃÇ*a×°ºÃÁË¡£ÕaÀi£¬ÎÒÃÇÐeÒªÓõ½DoctrineFixturesBundle¡£
µÚÒ»²½£¬ÔÚcomposer.jsonÖÐÒýÈeËuÐeµÄDoctrineFixturesBundle£º
{
"require": {
"doctrine/doctrine-fixtures-bundle": "2.2.*"
}
}
µÚ¶þ²½£¬Ö´ÐÐcomposer£º
composer update doctrine/doctrine-fixtures-bundle
µÚÈý²½£¬ÔÚÄںˣ¨app/AppKernel.php£(C)ÖÐ×¢²a´Ëbundle£º
// ...
public function registerBundles()
{
$bundles = array(
// ...
new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
// ...
);
// ...
}
µÚËIJ½£¬ÔÚÐeÒªÄÚÖÃÊý¾ÝµÄbundleÏ´´½¨Ò»¸oPHPÀaÎļþ£¬Èçsrc/Acme/HelloBundle/DataFixtures/ORM/LoadUserData.php£¬Æa´uÂeÈçÏ£º
// 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();
}
}
µÚÎa²½£¬Í¨¹ýconsoleÖ´ÐÐÄÚÖÃÊý¾ÝÃuÁi£º
php app/console doctrine:fixtures:load #Ϊ*ÀÖ¹Êý¾Ý¿aÖÐÔÏȵÄÖµ±»Ça³ý£¬¿ÉʹÓà --append ²ÎÊý
´ËÃuÁiÓÐÒÔÏÂÈý¸o²ÎÊý£º
¨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.
¹Ù*½Îĵµ£ºhttp://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html
±¾ÎÄÓÀ¾ÃµØÖ*£ºhttp://blog.it985.com/6662.html
±¾Îijo×Ô IT985²(C)¿Í £¬×ªÔØʱÇe×¢Ã÷³o´¦¼°ÏaÓ¦Á´½Ó¡£
¸u¶a¹ØÓÚPHP¿o¼ÜÏa¹ØÄÚÈݸÐÐËȤµÄ¶ÁÕ߿ɲe¿´±¾Õ¾×¨Ìa£º¡¶phpÓÅÐa¿ª*¢¿o¼Ü×ܽa¡£¬¡¶codeigniterÈeÃŽ̡̳£¬¡¶CI(CodeIgniter)¿o¼Ü½ø½×½Ì³Ì¡£¬¡¶Yii¿o¼ÜÈeÃż°³£Óü¼ÇÉ×ܽa¡¼°¡¶ThinkPHPÈeÃŽ̡̳*
Ï£Íu±¾ÎÄËuÊo¶Ô´o¼Ò»uÓÚSymfony¿o¼ÜµÄPHP³ÌÐoÉe¼ÆÓÐËu°iÖu¡£