Question

I'm implementing the classic Blog app with Symfony2, and the "app/console doctrine:fixtures:load" returns an error. My BlogFixtures.php file is like this:

<?php
namespace MGF\Bundles\WebBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use MGF\Bundles\WebBundle\Entity\Blog;
use MGF\Bundles\CRMBundle\Util\Util;

class BlogFixtures extends AbstractFixture implements FixtureInterface
{
    public function load(ObjectManager $em)
    {
        $blog1 = new Blog();

        $title = 'First post';
        $blog1->setTitle($title);

        $slug1 = Util::getSlug($title);
        $blog1->setSlug($slug1);

        $blog1->setImage('beach.jpg');
        $blog1->setTags('symfony2, php, paradise, symblog');
        $blog1->setCreated(new \DateTime('now'));
        $blog1->setUpdated($blog1->getCreated());

        $em->persist($blog1);

        $author1 = $em->getRepository('MGFBCBundle:User')->findOneByUser('sarah');
        $author1->addBlog($blog1);

        $em->persist($author1);
        $em->flush();
    }
}

And the error:

app/console doctrine:fixtures:load
Careful, database will be purged. Do you want to continue Y/N ?Y
  > purging database
  > loading MGF\Bundles\WebBundle\DataFixtures\ORM\BlogFixtures
PHP Fatal error:  Call to a member function addBlog() on a non-object in /var/www/METRO/src/MGF/Bundles/WebBundle/DataFixtures/ORM/BlogFixtures.php on line 33

Fatal error: Call to a member function addBlog() on a non-object in /var/www/METRO/src/MGF/Bundles/WebBundle/DataFixtures/ORM/BlogFixtures.php on line 33

I don't see where I go wrong. Any hints?

Thanks in advance.

Was it helpful?

Solution

The problem was that even though the user 'sarah' exists in the db from the fixtures, when trying to load fixtures again, db gets purged. So I needed to reference my users when created from the fixtures, and retrieve them by their reference, as explained here:

http://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html#sharing-objects-between-fixtures

Fixtures loading is working again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top