Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 条令';我不会坚持,为什么?_Php_Symfony_Doctrine Orm - Fatal编程技术网

Php 条令';我不会坚持,为什么?

Php 条令';我不会坚持,为什么?,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,我有一套小的数据装置,其设置如下: $tree-A code/src/AppBundle/DataFixtures/ORM/ 代码/src/AppBundle/DataFixtures/ORM/ ├── LoadCategoryData.php ├── LoadCategoryHasPostData.php └── LoadPostData.php 当我尝试加载装置时,请参见以下输出: $docker exec-u www-data-it-coding\u-webserver\u 1 php-

我有一套小的数据装置,其设置如下:

$tree-A code/src/AppBundle/DataFixtures/ORM/
代码/src/AppBundle/DataFixtures/ORM/
├── LoadCategoryData.php
├── LoadCategoryHasPostData.php
└── LoadPostData.php
当我尝试加载装置时,请参见以下输出:

$docker exec-u www-data-it-coding\u-webserver\u 1 php-bin/console原则:fixture:load-vvv
小心,数据库将被清除。是否要继续?否?是
>清除数据库
>正在加载[1]AppBundle\DataFixtures\ORM\LoadCategoryData
>正在加载[2]AppBundle\DataFixtures\ORM\LoadPostData
>正在加载[3]AppBundle\DataFixtures\ORM\LoadCategoryHasPostData
[Symfony\Component\Debug\Exception\fatalthrowayerror]
类型错误:传递给AppBundle\Entity\CategoryHasPosts::setCategory()的参数1必须是AppBundle\Entity\Category的实例,给定为空,在/var/www/src/AppBundle/DataFixtures/ORM/LoadCategory中调用
第22行的HasPostData.php
这是文件
LoadCategoryData
的外观:

类LoadCategoryData扩展AbstractFixture实现OrderedFixture接口
{
公共函数加载(ObjectManager$manager)
{
$category_1=新类别();
$category_1->setCategoryName('Hot');
$category_1->setCategoryDescription(“热门话题”);
$category_1->setSlug('hot');
$category_1->setActive(真);
$manager->persist($category_1);
$category_2=新类别();
$category_2->setCategoryName('New');
$category_2->setCategoryDescription(“新主题”);
$category_2->setSlug(“新”);
$category_2->setActive(真);
$manager->persist($category_2);
$manager->flush();
}
公共函数getOrder()
{
返回1;
}
}
这是
LoadPostData
文件的内容:

类LoadPostData扩展AbstractFixture实现OrderedFixtureInterface
{
公共函数加载(ObjectManager$manager)
{
$faker=faker\Factory::create();
对于($i=0;$i<1000;$i++){
$post=新的post();
$post->setPostTitle($faker->句子);
$post->setPostContent($faker->段落);
$post->setSlug($faker->slug);
$manager->persist($post);
}
$manager->flush();
}
公共函数getOrder()
{
返回2;
}
}
这是
LoadCategoryHasPostData
文件的内容:

类LoadCategoryHostData扩展AbstractFixture实现OrderedFixture接口
{
公共函数加载(ObjectManager$manager)
{
对于($i=0;$i<2000;$i++){
$category\u id=随机整数(1,2);
$category=$manager->getRepository('AppBundle:category')->find($category\u id);
$post_id=random_int(1,2000);
$post=$manager->getRepository('AppBundle:post')->find($post\u id);
$categoryHasPost=新的categoryHasPost();
$categoryHasPost->setCategory($category);
$categoryHasPost->setPost($post);
$manager->persist($categoryHasPost);
}
$manager->flush();
}
公共函数getOrder()
{
返回3;
}
}
我知道流程失败的原因及其逻辑:以前的实体没有持久化,这就是我的原因

为什么
Category
Post
没有持久化和刷新?我这里缺少什么?

试试看

类LoadCategoryHostData扩展AbstractFixture实现OrderedFixture接口{
公共函数加载(ObjectManager$manager)
{
$categories=$manager->getRepository('AppBundle:categority')->findAll();
$posts=$manager->getRepository('AppBundle:Post')->findAll();
对于($i=0;$i<2000;$i++){
$category=$categories[random_int(0,count$categories)-1];
$post=$posts[random_int(0,count$posts)-1);
$categoryHasPost=新的categoryHasPost();
$categoryHasPost->setCategory($category);
$categoryHasPost->setPost($post);
$manager->persist($categoryHasPost);
}
$manager->flush();
}
公共函数getOrder()
{
返回3;
}
}
您应该使用以下引用:

类LoadPostData扩展AbstractFixture实现OrderedFixtureInterface
{
公共函数加载(ObjectManager$manager)
{
$faker=faker\Factory::create();
对于($i=0;$i<1000;$i++){
$post=新的post();
$post->setPostTitle($faker->句子);
$post->setPostContent($faker->段落);
$post->setSlug($faker->slug);
$manager->persist($post);
$this->addReference('post.'$i,$post);
}
$manager->flush();
}
// …
}
类LoadCategoryHostData扩展AbstractFixture实现OrderedFixture接口
{
公共函数加载(ObjectManager$manager)
{
$categoryHasPost=新的categoryHasPost();
$categoryHasPost->setCategory($this->getReference('category_1'));
$categoryHasPost->setPost($this->getReference('post_1'));
$manager->persist($categoryHasPost);
$manager->flush();
}
// …
}

完美!我不知道问题到底出在哪里,你能编辑一下并解释一下吗?每次创建fixtures数据库时,ID都会增加。
$category\u ID=random\u int(1,2);
不确定数据库中的ID是1还是2。
/**@return category | null*/$category=$manager->getRepository('AppBundle:Category')->find($Category_id);
此返回AppB
class LoadCategoryData extends AbstractFixture implements OrderedFixtureInterface
{
    public function load(ObjectManager $manager)
    {
        $category_1 = new Category();
        // …
        $manager->persist($category_1);
        $this->addReference('category_1', $category_1);

        $manager->flush();
    }
    // …
}