0

I'm trying to load fixtures, and have exact id' to work with, so the code is:

    foreach ($this->categories as $index=>$category) {
        $newCategory = new Category();

        $metadata = $manager->getClassMetaData(get_class($newCategory));
        $metadata->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());            

        $newCategory->setId($index == 6 ? self::SOME_ID : ($index == 7 ? self::SOME_OTHER_ID : $index + 1));
        
        $manager->persist($newCategory);
        $this->setReference('CategoryRef-'.$index, $newCategory);
    }
    
    $manager->flush();

The thing is, that, when I'm loading reference to this Entity(Category) from another fixture(Product), I'm able to get id I'm looking for. But, mapping Product with Category fails and in case I'll exclude mapping to Category - the database will be still populated with auto-generated id values. So, the error code is:

Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`database`.`product`, CONSTRAINT `FK_D34A04AD12469DE2` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`))

It seems to me, that somehow, I'm working with 2 different id's: one - containing id I'm defining in code above, and the second one - generated by IdGenerator.

This question was also described in question, but, unfortunately, neither one solution had helped me.

Community
  • 1
  • 1
whalex
  • 23
  • 1
  • 5
  • Are you sure your nested ternary statement is giving you the correct values? If all you do is import categories then do you get the expected id's in the database? – Cerad Oct 30 '14 at 13:53
  • Yes, sure. No matter what will be passed to setId. – whalex Oct 31 '14 at 08:22

0 Answers0