0

我正在使用 Symfony2 实现经典的博客应用程序,并且“应用程序/控制台学说:fixtures:load”返回错误。我的 BlogFixtures.php 文件是这样的:

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

和错误:

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

我看不出我哪里错了。有什么提示吗?

提前致谢。

4

1 回答 1

1

问题是,即使用户 'sarah' 存在于来自固定装置的数据库中,当尝试再次加载固定装置时,数据库也会被清除。所以我需要在从固定装置创建时引用我的用户,并通过他们的引用检索它们,如下所述:

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

夹具加载再次正常工作。

于 2013-05-29T12:26:55.870 回答