我正在尝试使用Doctrine PHPCR DataFixtures,这里有一个参考示例。
<?php
namespace ExampleBundleCMSBundleDataFixturesPHPCR;
use DoctrineCommonDataFixturesAbstractFixture;
use DoctrineCommonDataFixturesOrderedFixtureInterface;
use DoctrineCommonPersistenceObjectManager;
use ExampleBundleCMSBundleDocumentTheme;
use PHPCRUtilNodeHelper;
class LoadThemeData extends AbstractFixture implements OrderedFixtureInterface
{
/**
* {@inheritDoc}
*/
public function load(ObjectManager $dm)
{
NodeHelper::createPath($dm->getPhpcrSession(), '/cms/themes');
$parent = $dm->find(null, '/cms/themes');
$theme = new Theme();
$theme->setTitle('Home');
$theme->setPath('/');
$theme->setParent($parent);
$dm->persist($theme);
$dm->flush();
$this->setReference('theme-default', $theme);
}
/**
* {@inheritDoc}
*/
public function getOrder()
{
return 1; // the order in which fixtures will be loaded
}
}
出现以下错误:
php app/console doctrine:phpcr:fixtures:load
Careful, database will be purged. Do you want to continue Y/N ?y
> purging database
> loading [1] ExampleBundleCMSBundleDataFixturesPHPCRLoadThemeData
PHP Fatal error: Call to undefined method DoctrineODMPHPCRUnitOfWork::isInIdentityMap() in /Volumes/Project/CMS/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/ReferenceRepository.php on line 97
PHP Stack trace:
PHP 1. {main}() /Volumes/Project/CMS/app/console:0
PHP 2. SymfonyComponentConsoleApplication->run() /Volumes/Project/CMS/app/console:26
PHP 3. SymfonyBundleFrameworkBundleConsoleApplication->doRun() /Volumes/Project/CMS/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:121
PHP 4. SymfonyComponentConsoleApplication->doRun() /Volumes/Project/CMS/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:96
PHP 5. SymfonyComponentConsoleApplication->doRunCommand() /Volumes/Project/CMS/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:191
PHP 6. SymfonyComponentConsoleCommandCommand->run() /Volumes/Project/CMS/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:904
PHP 7. DoctrineBundlePHPCRBundleCommandLoadFixtureCommand->execute() /Volumes/Project/CMS/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:244
PHP 8. DoctrineCommonDataFixturesExecutorPHPCRExecutor->execute() /Volumes/Project/CMS/vendor/doctrine/phpcr-bundle/Doctrine/Bundle/PHPCRBundle/Command/LoadFixtureCommand.php:111
PHP 9. DoctrineCommonDataFixturesExecutorAbstractExecutor->load() /Volumes/Project/CMS/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Executor/PHPCRExecutor.php:65
PHP 10. ExampleBundleCMSBundleDataFixturesPHPCRLoadThemeData->load() /Volumes/Project/CMS/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Executor/AbstractExecutor.php:121
PHP 11. DoctrineCommonDataFixturesAbstractFixture->setReference() /Volumes/Project/CMS/src/Example/Bundle/CMSBundle/DataFixtures/PHPCR/LoadThemeData.php:31
PHP 12. DoctrineCommonDataFixturesReferenceRepository->setReference() /Volumes/Project/CMS/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/AbstractFixture.php:60
深入研究一下这个问题,DoctrineFixturesBundle用于ODM和ORM。。因此,如果你同时查看Doctrine\ODM\PHPR\UnitOfWork和Doctrine\DM\MongoDB \UnitOfWork,它们都有一个$identityMap变量。。
Doctrine\ODM\PHPCR\UnitOfWork缺少isInIdentityMap函数。。
有什么建议吗?我下一步该怎么办?
我只想与大家分享我解决这个问题的方法。
- 我使用
OrderedFixtureInterface
接口强制订单 - 我用
ObjectManager
的find
方法加载参考对象,例如:$home->setContent($dm->find(null, '/cms/pages/home'));
完整示例:
文件1:
<?php
namespace PagePortfolioBundleDataFixturesPHPCR;
use PagePortfolioBundleDocumentPage;
use DoctrineCommonDataFixturesAbstractFixture;
use DoctrineCommonDataFixturesOrderedFixtureInterface;
use DoctrineCommonPersistenceObjectManager;
class LoadPageData extends AbstractFixture implements OrderedFixtureInterface
{
public function getOrder()
{
return 100;
}
public function load(ObjectManager $dm)
{
$root = $dm->find(null, '/cms/pages');
$home = new Page();
$home->setNodeName('home');
$home->setParentDocument($root);
$home->setTitle('Home');
$home->setContent('Welcome to the homepage of this really basic CMS.');
$dm->persist($home);
$dm->flush();
}
}
文件2:
<?php
namespace PagePortfolioBundleDataFixturesPHPCR;
use DoctrineCommonDataFixturesAbstractFixture;
use DoctrineCommonDataFixturesOrderedFixtureInterface;
use DoctrineCommonPersistenceObjectManager;
use SymfonyCmfBundleMenuBundleDoctrinePhpcrMenu;
use SymfonyCmfBundleMenuBundleDoctrinePhpcrMenuNode;
class LoadMainMenuData extends AbstractFixture implements OrderedFixtureInterface
{
public function getOrder()
{
return 500;
}
public function load(ObjectManager $dm)
{
$root = $dm->find(null, '/cms/menu');
$menu = new Menu();
$menu->setParentDocument($root);
$menu->setName('main');
$menu->setLabel('Main Menu');
$dm->persist($menu);
$dm->flush();
$home = new MenuNode();
$home->setParentDocument($menu);
$home->setName('home');
$home->setLabel('Home');
$home->setLinkType('content');
$home->setContent($dm->find(null, '/cms/pages/home'));
$dm->persist($home);
$dm->flush();
}
}