I’m trying to create an entity in Symfony2 that the primary key based on another table, but I am getting the following error when trying to run the data fixtures.
[Doctrine\ORM\ORMException]
Entity of type Ofdan\SearchBundle\Entity\CacheHeader is missing an assigned ID. The identifier generation strategy for this entity requires the ID field to be populated before EntityManager#persist() is called. If you want automatically generated identifiers instead you need to adjust the metadata mapping accordingly.
/**
* @ORM\Entity
* @ORM\Table(name="cacheHeader")
* @ORM\HasLifecycleCallbacks
*/
class CacheHeader
{
/**
* @ORM\ManyToOne(targetEntity="Domain", inversedBy="domains")
* @ORM\JoinColumn(name="domainId", referencedColumnName="id")
* @ORM\Id
*/
private $domain;
...
}
The data fixture I’m using loads in the domain, which should be setting the domainId, which should mean there is an Id.
class CacheHeaderFixtures extends AbstractFixture implements OrderedFixtureInterface
{
public function load(ObjectManager $manager)
{
$cacheHeader1 = new CacheHeader();
$cacheHeader1->setDomain($manager->merge($this->getReference('bbc-domain')));
$cacheHeader1->setDate(new \DateTime());
$cacheHeader1->setPage(10); // CacheHeader::Page_Index
$cacheHeader1->setHeader('');
$manager->persist($cacheHeader1);
}
}
How can I get my CacheHeader entity to work with the domainId?
The answer is the above works.
The reason I had issue was I had an second fixture, but called setDomain on the wrong object.