1
votes

I am trying to use the Doctrine PHPCR DataFixtures with a reference here is an example.

<?php

namespace Example\Bundle\CMSBundle\DataFixtures\PHPCR;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;

use Example\Bundle\CMSBundle\Document\Theme;
use PHPCR\Util\NodeHelper;

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
    }
}

The Following error occurs:

php app/console doctrine:phpcr:fixtures:load
Careful, database will be purged. Do you want to continue Y/N ?y
  > purging database
  > loading [1] Example\Bundle\CMSBundle\DataFixtures\PHPCR\LoadThemeData
PHP Fatal error:  Call to undefined method Doctrine\ODM\PHPCR\UnitOfWork::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. Symfony\Component\Console\Application->run() /Volumes/Project/CMS/app/console:26
PHP   3. Symfony\Bundle\FrameworkBundle\Console\Application->doRun() /Volumes/Project/CMS/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:121
PHP   4. Symfony\Component\Console\Application->doRun() /Volumes/Project/CMS/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:96
PHP   5. Symfony\Component\Console\Application->doRunCommand() /Volumes/Project/CMS/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:191
PHP   6. Symfony\Component\Console\Command\Command->run() /Volumes/Project/CMS/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:904
PHP   7. Doctrine\Bundle\PHPCRBundle\Command\LoadFixtureCommand->execute() /Volumes/Project/CMS/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:244
PHP   8. Doctrine\Common\DataFixtures\Executor\PHPCRExecutor->execute() /Volumes/Project/CMS/vendor/doctrine/phpcr-bundle/Doctrine/Bundle/PHPCRBundle/Command/LoadFixtureCommand.php:111
PHP   9. Doctrine\Common\DataFixtures\Executor\AbstractExecutor->load() /Volumes/Project/CMS/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Executor/PHPCRExecutor.php:65
PHP  10. Example\Bundle\CMSBundle\DataFixtures\PHPCR\LoadThemeData->load() /Volumes/Project/CMS/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Executor/AbstractExecutor.php:121
PHP  11. Doctrine\Common\DataFixtures\AbstractFixture->setReference() /Volumes/Project/CMS/src/Example/Bundle/CMSBundle/DataFixtures/PHPCR/LoadThemeData.php:31
PHP  12. Doctrine\Common\DataFixtures\ReferenceRepository->setReference() /Volumes/Project/CMS/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/AbstractFixture.php:60

Looking into the issue a bit more the DoctrineFixturesBundle is used for ODM and ORM.. So if you look at both Doctrine\ODM\PHPCR\UnitOfWork and Doctrine\ODM\MongoDB\UnitOfWork, they both have a $identityMap variable..

Doctrine\ODM\PHPCR\UnitOfWork is missing the isInIdentityMap function..

Any suggestions? what should i do next?

1
Did you ever find a solution to this?Matt
no i have not found one888
Okay, I ended up injecting the doctrine_phpcr service and just getting the documents that way. Using that with the ordered fixtures let me do what I needed.Matt

1 Answers

0
votes

I just want to share with my workaround to this issue.

  1. I use OrderedFixtureInterface interface to force order.
  2. I load reference object with find method of ObjectManager, e.g.: $home->setContent($dm->find(null, '/cms/pages/home'));

Full example:

File 1:

<?php
namespace Page\PortfolioBundle\DataFixtures\PHPCR;

use Page\PortfolioBundle\Document\Page;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;

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();
    }
}

File 2:

<?php
namespace Page\PortfolioBundle\DataFixtures\PHPCR;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Cmf\Bundle\MenuBundle\Doctrine\Phpcr\Menu;
use Symfony\Cmf\Bundle\MenuBundle\Doctrine\Phpcr\MenuNode;

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();
    }
}