销售,库存和目录之间存在双向关系。销售和库存应该有一个商品。但目前情况并非总是如此......所以关系是“可空的”。
class Sale {
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Stock", inversedBy="sales")
*/
private $stock;
public function getCatalog(): ?Catalog
{
return $this->catalog;
}
public function setCatalog(?Catalog $catalog): self
{
$this->catalog = $catalog;
return $this;
}
}
class Stock
{
/**
* @ORM\OneToOne(targetEntity="App\Entity\Catalog", inversedBy="stock")
*/
private $catalog;
public function getCatalog(): ?Catalog
{
return $this->catalog;
}
public function setCatalog(?Catalog $catalog): self
{
$this->catalog = $catalog;
return $this;
}
}
class Catalog
{
/**
* @ORM\OneToMany(targetEntity="App\Entity\Sale", mappedBy="catalog")
*/
private $sales;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Stock", 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将Sales和Stock中的category_id设置为null(UPDATE sale / stock SET category_id = null WHERE category_id = X)。但我认为这不是常见的'orm方式'。
对于这种情况,自动生成的函数对我来说有点奇怪。此处的学说文档qazxsw poi
我曾经读过,只有拥有方负责协会,但我如何进入我的职能股票和所有销售,如果有的话?
对不起,这是一个很长的问题和一个基本主题
谢谢您最好的问候
我的问题被标记为重复。主要区别在于
我想知道在我的问题中,如何实现函数调用以将所拥有的实体(两个或多个)设置为null。删除过程。哪个实体,什么样的电话。只添加
https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/working-with-associations.html#removing-associations
(旧答案)不解决问题。
--------- DQL替代方法----------
@ORM\JoinColumn(name="catalog_id", referencedColumnName="id", onDelete="SET NULL")
我有史以来最长的职位;)
解决方案确实是这个简单的设置。它也可以是单向关系。
// 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 App\Entity\Sale s
SET s.catalog = NULL
WHERE s.catalog = :id
')
->setParameter('id', $id)
->execute()
;
$this->getEntityManager()
->createQuery(/** @lang DQL */'
UPDATE App\Entity\Stock s
SET s.catalog = NULL
WHERE s.catalog = :id
')
->setParameter('id', $id)
->execute()
;
$numDeleted = $this->getEntityManager()
->createQuery(/** @lang DQL */'
DELETE FROM App\Entity\Catalog c
WHERE c.id = :id
')
->setParameter('id', $id)
->execute()
;
return $numDeleted;
}
然后控制器功能很好用。昨天我尝试了太多东西......