[Flow] PeristenceManager->remove() ends up in integrity constraints violation exception
Carsten Bleicker
carsten at bleicker.de
Mon Dec 16 11:22:08 CET 2013
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)
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?
If the code doesnt get displayed well formated it is also available here:
https://gist.github.com/pumatertion/05424c2541c9d8ec8bba
// PostalAddress
class PostalAddress{
/**
* @var \Doctrine\Common\Collections\Collection<\BLEICKER\Sale\Domain\Model\Company>
* @ORM\OneToMany(mappedBy="postalAddress",cascade={"detach", "merge", "remove"})
* @ORM\OrderBy({"name" = "ASC"})
*/
protected $companies;
}
// Company
class Company extends AbstractManufacturer {
/**
* @var PostalAddress
* @ORM\Column(nullable=true)
* @ORM\ManyToOne(cascade={"persist", "detach", "merge"})
*/
protected $postalAddress;
}
// PostalAddressController running in exception
class PostalAddressController{
/**
* @param PostalAddress $postalAddress
* @return void
*/
public function deleteAction(PostalAddress $postalAddress) {
$this->persistenceManager->remove($postalAddress);
$this->addFlashMessage('Postal address deleted', Message::SEVERITY_OK, Message::SEVERITY_OK, array(), 1387120896);
$this->redirect('index');
}
}
// PostalAddressController NOT running in exception
class PostalAddressController{
/**
* @param PostalAddress $postalAddress
* @return void
*/
public function deleteAction(PostalAddress $postalAddress) {
$companyIterator = $postalAddress->getCompanies()->getIterator();
while ($companyIterator->valid()) {
/**
* @var Company $company
*/
$company = $companyIterator->current();
$company->setPostalAddress();
$this->persistenceManager->update($company);
$this->addFlashMessage('Postal address removed from company "' . $company->getName() . '"', Message::SEVERITY_WARNING, Message::SEVERITY_WARNING, array(), 1387120895);
$companyIterator->next();
}
$postalAddress->getCompanies()->clear();
$this->persistenceManager->remove($postalAddress);
$this->persistenceManager->persistAll();
$this->addFlashMessage('Postal address deleted', Message::SEVERITY_OK, Message::SEVERITY_OK, array(), 1387120896);
$this->redirect('index');
}
}
More information about the Flow
mailing list