I have 3 entity : Invoice, InvoiceItemService, Asset.
First, I create an Invoice and an InvoiceItemService, and I link them together with a ManyToOne relation on InvoiceItemService side (so InvoiceItemService is the owner side).
Then, I can create an Asset, from the Invoice object. An asset is kind of a copy of an Invoice, with a negative total. When I create an Asset, I link the Invoice's InvoiceItemService to the Asset too, with a ManyToOne relation between InvoiceItemService and Asset.
When I delete an Invoice, everything works fine, Invoice, Asset and InvoiceItemService are deleted with a cascade={"remove"} option.
When I delete an Asset, I would like only the asset to be deleted. But I get this foreign key error.
Here are my entities :
class Invoice { /** * @ORM\OneToMany(targetEntity="Evo\BackendBundle\Entity\Asset", mappedBy="invoice", cascade={"remove"}) */ protected $assets; /** * @ORM\OneToMany(targetEntity="Evo\BackendBundle\Entity\InvoiceItemService", mappedBy="invoice", cascade={"persist", "remove"}) */ protected $services; } class Asset { /** * @ORM\ManyToOne(targetEntity="Evo\BackendBundle\Entity\Invoice", inversedBy="assets") * @ORM\JoinColumn(nullable=false) * @Exclude */ protected $invoice; /** * @ORM\OneToMany(targetEntity="Evo\BackendBundle\Entity\InvoiceItemService", mappedBy="asset") */ protected $services; } class InvoiceItemService extends InvoiceItem { /** * @ORM\ManyToOne(targetEntity="Evo\BackendBundle\Entity\Invoice", inversedBy="services") * @ORM\JoinColumn(nullable=false, onDelete="CASCADE") * @Type("Evo\BackendBundle\Entity\Invoice") * @Exclude */ protected $invoice; /** * @ORM\ManyToOne(targetEntity="Evo\BackendBundle\Entity\Asset", inversedBy="services") * @ORM\JoinColumn(nullable=true, onDelete="SET NULL") * @Type("Evo\BackendBundle\Entity\Asset") * @Exclude */ protected $asset; }
Assetfor a foreign key? Your error may be entirely unrelated to the relationship between these three you posted here.a foreign key constraint fails (`evotest`.`sf_invoices_items_services`, CONSTRAINT `FK_38F5C7765DA1941` FOREIGN KEY (`asset_id`) REFERENCES `sf_assets` (`id`))Actually, i would need a doctrine option to setasset_idto NULL in InvoiceItemService, when an asset is deleted. tried@ORM\JoinColumn(nullable=true, onDelete="SET NULL")in InvoiceItemService#asset property, but error still there