[Flow] PeristenceManager->remove() ends up in integrity constraints violation exception
Andreas Wolf
aw at foundata.net
Sat Jan 4 14:12:50 CET 2014
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Hi Carsten,
Am 16.12.2013 11:22, schrieb Carsten Bleicker:
> I dont get it :( Shouldnt "detach" automaticaly release relations
> on delete? Instead of this i am runnig in an exception:
>
> SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete
> or update a parent row: a foreign key constraint fails
> (`bleicker_sale_development`.`bleicker_sale_domain_model_company`,
> CONSTRAINT `FK_20EA261F2B5A4883` FOREIGN KEY (`postaladdress`)
> REFERENCES `bleicker_sale_domain_model_postaladdress` (`persist)
the root cause here is that you're not removing the object on the
owning side of the relation. The concept of "owning side" in Doctrine
says that each relation has an owning side. If you want to delete a
relation, you have to do that from its owning side, otherwise you will
run into the mentioned SQL error.
For a 1:n relation, Doctrine will always put the owning side on the
n-side of the relation. This means that there will be no intermediate
table, because you can always store *one* identifier of a related
object in a field, but not more than one (which you possible have on
the 1-side of the relation).
> There is also a doctrine annotation "refresh" but it also has no
> effect. Releasing the relation from Company->PostalAddress inside
> of the deletePostalAddressAction works. But is this realy a proof
> concept? How do you solve this?
For a 1:1 relation, just removing the postal address from the company
and deleting it afterwards will work. As you have a 1:n relation from
PostalAddress to Company, the PostalAddress has to be removed from all
Company objects.
Sidenote:
Doctrine in fact saves you from a possibly unwanted behaviour here:
The database constraint installed by Doctrine makes deletion of the
PostalAddress row impossible as long as there is still a Company
object referencing it.
In your specific use case, I do not quite understand the necessity to
drop a PostalAddress. My approach would be to remove the PostalAddress
from the company (in CompanyController), and probably once in a while
remove dangling PostalAddress objects (the ones without any Company
attached to them). But probably your use case is different than what I
imagine ;-)
Cheers
Andreas
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
iF4EAREIAAYFAlLICNIACgkQVRefK2MMWg7cEAEAhBAZLQXR2a8CKFYJZvM8GjsB
LbYOk5+XY5YomeZKSGEA/A/2Gp9TAkMgZmJk5HhEKyL3XFlIKPJ0tPRDmnSg1njh
=x4km
-----END PGP SIGNATURE-----
More information about the Flow
mailing list