我正在使用Symfony2实现经典博客应用程序,并且"app/console学说:fixtures:load"返回错误。我的BlogFixtures.php文件是这样的:
<?php
namespace MGFBundlesWebBundleDataFixturesORM;
use DoctrineCommonDataFixturesAbstractFixture;
use DoctrineCommonDataFixturesFixtureInterface;
use DoctrineCommonPersistenceObjectManager;
use MGFBundlesWebBundleEntityBlog;
use MGFBundlesCRMBundleUtilUtil;
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 MGFBundlesWebBundleDataFixturesORMBlogFixtures
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
我不知道我错在哪里。有提示吗?
问题是,即使用户'sarah'存在于来自fixture的db中,当试图再次加载fixture时,db被清除。因此,我需要在从fixture创建用户时引用它们,并通过它们的引用检索它们,如下所示:
http://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html sharing-objects-between-fixtures
设备加载正常