如何删除具有两个关系的'inverse-side entiy'



销售、库存和目录之间存在双向关系。 销售和库存,应具有目录条目。但是这个 目前并非总是如此...因此,这些关系是"可为空的"。

class Sale {
/**
* @ORMManyToOne(targetEntity="AppEntityStock", inversedBy="sales")
*/
private $stock;
public function getCatalog(): ?Catalog
{
return $this->catalog;
}
public function setCatalog(?Catalog $catalog): self
{
$this->catalog = $catalog;
return $this;
}
}
class Stock
{
/**
* @ORMOneToOne(targetEntity="AppEntityCatalog", inversedBy="stock")
*/
private $catalog;
public function getCatalog(): ?Catalog
{
return $this->catalog;
}
public function setCatalog(?Catalog $catalog): self
{
$this->catalog = $catalog;
return $this;
}
}
class Catalog
{
/**
* @ORMOneToMany(targetEntity="AppEntitySale", mappedBy="catalog")
*/
private $sales;
/**
* @ORMOneToOne(targetEntity="AppEntityStock", mappedBy="catalog")
*/
private $stock;
public function addSale(Sale $sale): self
{
if (!$this->sales->contains($sale)) {
$this->sales[] = $sale;
$sale->setCatalog($this);
}
return $this;
}
public function removeSale(Sale $sale): self
{
if ($this->sales->contains($sale)) {
$this->sales->removeElement($sale);
// set the owning side to null (unless already changed)
if ($sale->getCatalog() === $this) {
$sale->setCatalog(null);
}
}
return $this;
}
public function getStock(): ?Stock
{
return $this->stock;
}
public function setStock(?Stock $stock): self
{
$this->stock = $stock;
// set (or unset) the owning side of the relation if necessary
$newCatalog = $stock === null ? null : $this;
if ($newCatalog !== $stock->getCatalog()) {
$stock->setCatalog($newCatalog);
}
return $this;
}
}

所有这些都是在Symfony中自动生成的,带有make:entity。组织时 目录,还需要删除目录条目。

class CatalogController extends AbstractController 
{
/**
* @Route(
*     path    = "/catalog-delete/{id<[1-9]d*>}",
*     name    = "catalog_delete",
*     methods = {"GET"}
* )
*/
public function delete(int $id)
{
// get catalog
$catalog = $this->catalogRepository->find($id);
if (!$catalog) {
throw $this->createNotFoundException($this->translator->trans('system.error.notfound') . $id);
}
// delete
$this->entityManager->remove($catalog);
$this->entityManager->flush();
return $this->redirectToRoute('catalog_list');
}
}

到目前为止非常简单。但是如何在不删除的情况下删除关联 任何股票或销售?我确实在这里收到这样的错误(不删除关联)

Cannot delete or update a parent row: a foreign key constraint fails (`symfony`.`sale`, CONSTRAINT `FK_E54BC005DCD6110` FOREIGN KEY (`stock_id`) REFERENCES `stock` (`id`))

或喜欢

Call to a member function getCatalog() on null

当我尝试在函数 delete() ;)

$catalog->setStock(null);
foreach($catalog->getSales() as $sale) {
$catalog->getSales()->removeElement($sale); // ????????
}

我可以使用 DQL category_id在 销售和库存为空(更新销售/库存设置category_id = 空,其中 category_id = X)。 但我认为这不是常见的"orm way"。

对于这种情况,自动生成的函数对我来说有点奇怪 还有这里的教义文件 https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/working-with-associations.html#removing-associations

我读过,只有拥有方对协会负责, 但是我如何进入我的函数库存和所有销售(如果有的话)?

很抱歉这个长问题和一个基本话题。

谢谢你和最好的问候


我的问题被标记为重复。主要区别在于

我想知道在我的问题中,如何实现函数调用以将拥有的实体(两个或多个)设置为 null。删除过程。哪个实体,什么样的调用。仅添加

@ORMJoinColumn(name="catalog_id", referencedColumnName="id", onDelete="SET NULL")  

(旧答案)不能解决问题。

---------DQL 解决方法----------


// Controller
/**
* @Route(
*     path    = "/catalog-delete/{id<[1-9]d*>}",
*     name    = "catalog_delete",
*     methods = {"GET"}
* )
*/
public function delete(int $id)
{
// get catalog
$numDeleted = $this->catalogRepository->delete($id);
if (!$numDeleted) {
throw $this->createNotFoundException($this->translator->trans('system.error.notfound') . $id);
}
return $this->redirectToRoute('catalog_list');
}
// Repository
/**
* Delete with associations
*/
public function delete(int $id): int
{
$this->getEntityManager()
->createQuery(/** @lang DQL */'
UPDATE AppEntitySale s 
SET s.catalog = NULL
WHERE s.catalog = :id 
')
->setParameter('id', $id)
->execute()
;
$this->getEntityManager()
->createQuery(/** @lang DQL */'
UPDATE AppEntityStock s 
SET s.catalog = NULL
WHERE s.catalog = :id 
')
->setParameter('id', $id)
->execute()
;
$numDeleted = $this->getEntityManager()
->createQuery(/** @lang DQL */'
DELETE FROM AppEntityCatalog c
WHERE c.id = :id 
')
->setParameter('id', $id)
->execute()
;
return $numDeleted;
}

我有史以来最长的帖子;)

解决方案确实是这个简单的设置。 它也可以是单向关系。

class Sale {
/**
* @ORMManyToOne(targetEntity="AppEntityStock")
* @ORMJoinColumn(onDelete="SET NULL")
*/
private $stock;
public function getCatalog(): ?Catalog
{
return $this->catalog;
}
public function setCatalog(?Catalog $catalog): self
{
$this->catalog = $catalog;
return $this;
}
}
class Stock
{
/**
* @ORMOneToOne(targetEntity="AppEntityCatalog")
* @ORMJoinColumn(onDelete="SET NULL")
*/
private $catalog;
public function getCatalog(): ?Catalog
{
return $this->catalog;
}
public function setCatalog(?Catalog $catalog): self
{
$this->catalog = $catalog;
return $this;
}
}
class Catalog
{
}

然后控制器功能工作得很好。昨天我尝试了太多东西...

相关内容

  • 没有找到相关文章

最新更新