0
我正在用Symfony2實現經典的Blog應用程序,而「app/console doctrine:fixtures:load」會返回一個錯誤。我BlogFixtures.php文件是這樣的:加載Symfony2裝置時調用非成員函數的錯誤
<?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
我沒有看到我走到哪裏錯了。任何提示?
在此先感謝。
不用說'sarah'用戶確實存在於db中,我能夠在每個控制器中檢索它。 – gomman
var_dump($ author1)說什麼? –
@ Teo.sk是否可以在fixtures文件中var_dump?控制檯是否打印結果? (我afk,不會回來45分鐘,我會嘗試以後) – gomman