DDC-1064: Load fixtures with AbstractFixture (twice records) #1328

Open
opened 2026-01-22 13:10:34 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Mar 8, 2011).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user garfield-fr:

I read this documentation to import my fixtures data on chapter "Sharing objects between fixtures" https://github.com/doctrine/data-fixtures.
After this import, i saw 2 records on table "Blog" and single record on table "BlogComment".

If i comment the first flush on BlogFixtures, i have only one record on table "Blog". The second flush on BlogCommentFixtures save another record on table "Blog".

my code:

namespace Test\apcBundle\DataFixtures\ORM;

use Doctrine\ORM\EntityManager;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;

use Test\apcBundle\Entity\Blog;

class BlogFixtures extends AbstractFixture implements OrderedFixtureInterface
{
    public function load($manager)
    {
        $p = new Blog();
        $p->setTitle('title_1');
        $p->setDescription('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
 et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
 consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
 occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.');

        $manager->persist($p);
        $manager->flush();

        $this->addReference('blog', $p);
    }

    public function getOrder()
    {
        return 10;
    }
}

namespace Test\apcBundle\DataFixtures\ORM;

use Doctrine\ORM\EntityManager;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;

use Test\apcBundle\Entity\BlogComment;

class BlogCommentFixtures extends AbstractFixture implements OrderedFixtureInterface
{
    public function load($manager)
    {
        $bc = new BlogComment();
        $bc->setTitle('blogComment_1');
        $bc->setDescription('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.');
        $bc->setBlog($this->getReference('blog'));

        $manager->persist($bc);
        $manager->flush();
    }

    public function getOrder()
    {
        return 20;
    }
}
Originally created by @doctrinebot on GitHub (Mar 8, 2011). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user garfield-fr: I read this documentation to import my fixtures data on chapter "Sharing objects between fixtures" https://github.com/doctrine/data-fixtures. After this import, i saw 2 records on table "Blog" and single record on table "BlogComment". If i comment the first flush on BlogFixtures, i have only one record on table "Blog". The second flush on BlogCommentFixtures save another record on table "Blog". my code: ``` namespace Test\apcBundle\DataFixtures\ORM; use Doctrine\ORM\EntityManager; use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Common\DataFixtures\OrderedFixtureInterface; use Test\apcBundle\Entity\Blog; class BlogFixtures extends AbstractFixture implements OrderedFixtureInterface { public function load($manager) { $p = new Blog(); $p->setTitle('title_1'); $p->setDescription('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'); $manager->persist($p); $manager->flush(); $this->addReference('blog', $p); } public function getOrder() { return 10; } } ``` ``` namespace Test\apcBundle\DataFixtures\ORM; use Doctrine\ORM\EntityManager; use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Common\DataFixtures\OrderedFixtureInterface; use Test\apcBundle\Entity\BlogComment; class BlogCommentFixtures extends AbstractFixture implements OrderedFixtureInterface { public function load($manager) { $bc = new BlogComment(); $bc->setTitle('blogComment_1'); $bc->setDescription('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'); $bc->setBlog($this->getReference('blog')); $manager->persist($bc); $manager->flush(); } public function getOrder() { return 20; } } ```
admin added the Bug label 2026-01-22 13:10:34 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1328