夹具 DOCTRINE2
Fixture DOCTRINE2
当我使用时:
php app/console doctrine:fixtures:load --fixtures=/var/www/Symfony/src/BISSAP/ForumBundle/DataFixtures/ORM**
我收到以下错误:
PHP Catchable fatal error: Argument 1 passed to
BISSAP\ForumBundle\Entity\Forum::setCategory() must be an instance of
BISSAP\ForumBundle\Entity\Category, null given, called in
/var/www/Symfony/src/BISSAP/ForumBundle/DataFixtures/ORM/LoadForum.php
on line 40 and defined in
/var/www/Symfony/src/BISSAP/ForumBundle/Entity/Forum.php on line 184
我的夹具 - LoadForum.php:
<?php
namespace BISSAP\ForumBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use BISSAP\ForumBundle\Entity\Forum;
use BISSAP\ForumBundle\Entity\Category;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class LoadForum extends Controller implements FixtureInterface
{
public function load(ObjectManager $manager)
{
$data=array(array('NAME','DESCRTIPTION','60',$manager->getRepository('BISSAPForumBundle:Category')->find('1')),
array('NAME2','DESCRTIPTION2','60',$manager->getRepository('BISSAPForumBundle:Category')->find('2')));
foreach ($data as $for) {
$forum = new Forum();
$forum->setName($for[0]);
$forum->setDescription($for[1]);
$forum->setOrdre($for[2]);
$forum->setCategory($for[3]);
$manager->persist($forum);
}
$manager->flush();
}
}
doctrine:fixtures:load 擦除数据库中的所有数据并加载新的 fixtures
我相信你的问题是
$manager->getRepository('BISSAPForumBundle:Category')->find('1')
return 空结果而不是类别对象
看起来您要么在类别之前加载了论坛固定装置,要么您没有考虑到数据库已被删除并且您没有类别的任何记录。
对于情况 1,您应该更改灯具加载顺序 - 更改类别灯具的功能 "getOrder" 并设置 returned 编号低于论坛
上的编号
对于情况 2,您还应该为某些类别创建灯具
顺便说一句,你应该使用对象的引用,而不是从存储库中获取,所以常见的方法是:
为类别夹具创建新参考
$类别=新\MyApp\CategoryBundle\Entity\Category();
$category->setName();
$this->addReference('MyApp\CategoryBundle\Entity\Category-1',$category);
调用创建的引用以填充论坛
$forum->setCategory($this->getReference('MyApp\CategoryBundle\Entity\Category-1'));
当我使用时:
php app/console doctrine:fixtures:load --fixtures=/var/www/Symfony/src/BISSAP/ForumBundle/DataFixtures/ORM**
我收到以下错误:
PHP Catchable fatal error: Argument 1 passed to BISSAP\ForumBundle\Entity\Forum::setCategory() must be an instance of BISSAP\ForumBundle\Entity\Category, null given, called in /var/www/Symfony/src/BISSAP/ForumBundle/DataFixtures/ORM/LoadForum.php on line 40 and defined in /var/www/Symfony/src/BISSAP/ForumBundle/Entity/Forum.php on line 184
我的夹具 - LoadForum.php:
<?php
namespace BISSAP\ForumBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use BISSAP\ForumBundle\Entity\Forum;
use BISSAP\ForumBundle\Entity\Category;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class LoadForum extends Controller implements FixtureInterface
{
public function load(ObjectManager $manager)
{
$data=array(array('NAME','DESCRTIPTION','60',$manager->getRepository('BISSAPForumBundle:Category')->find('1')),
array('NAME2','DESCRTIPTION2','60',$manager->getRepository('BISSAPForumBundle:Category')->find('2')));
foreach ($data as $for) {
$forum = new Forum();
$forum->setName($for[0]);
$forum->setDescription($for[1]);
$forum->setOrdre($for[2]);
$forum->setCategory($for[3]);
$manager->persist($forum);
}
$manager->flush();
}
}
doctrine:fixtures:load 擦除数据库中的所有数据并加载新的 fixtures
我相信你的问题是
$manager->getRepository('BISSAPForumBundle:Category')->find('1')
return 空结果而不是类别对象
看起来您要么在类别之前加载了论坛固定装置,要么您没有考虑到数据库已被删除并且您没有类别的任何记录。
对于情况 1,您应该更改灯具加载顺序 - 更改类别灯具的功能 "getOrder" 并设置 returned 编号低于论坛
上的编号对于情况 2,您还应该为某些类别创建灯具
顺便说一句,你应该使用对象的引用,而不是从存储库中获取,所以常见的方法是:
为类别夹具创建新参考
$类别=新\MyApp\CategoryBundle\Entity\Category();
$category->setName();
$this->addReference('MyApp\CategoryBundle\Entity\Category-1',$category);
调用创建的引用以填充论坛
$forum->setCategory($this->getReference('MyApp\CategoryBundle\Entity\Category-1'));