Here is example of mapping:
Cateogory mapping:
<entity name="AppCoreDomainPostItem" table="post_item">
<id name="id" type="integer" column="id">
<generator strategy="SEQUENCE"/>
</id>
<field name="title" type="string" length="256" />
<many-to-one field="category"
target-entity="AppCoreDomainPostCategory"
inversed-by="items"
>
<join-column name="category_id" />
</many-to-one>
</entity>
Item mapping:
<entity name="AppCoreDomainPostCategory" table="post_category">
<id name="id" type="integer" column="id">
<generator strategy="SEQUENCE"/>
</id>
<field name="title" type="string" length="256" />
<one-to-many
field="items"
target-entity="AppCoreDomainPostItem"
mapped-by="category"
fetch="EXTRA_LAZY"
>
<cascade><cascade-all/></cascade>
</one-to-many>
</entity>
Im trying to save item entity with category entity:
<?php
//...
$category = $em->getRepository(Category::class)->find($categoryId);
//category exists and received as Category object
$item = new Item();
$item->setTitle('test')
$item->setCategory($category);
$em->persist($item);
$em->flush();
Getting error:
A new entity was found through the relationship 'AppCoreDomainPostItem#category' that was
not configured to cascade persist operations for entity: TestCategory. To solve this issue: Either explicitly call Enti
tyManager#persist() on this unknown entity or configure cascade persist this association in the mapping for exa
mple @ManyToOne(..,cascade={"persist"})
If I trying with category reference:
$category = $em->getReFerence(Category::class, $categoryId);
$item = new Item();
$item->setTitle('test')
$item->setCategory($category);
$em->persist($item);
$em->flush();
Record in database saved, but category_id field is null
What am i doing wrong?