Que*_*.D. 6 php unit-of-work symfony doctrine-orm
我有 3 个实体:
文件夹 :
<?php
namespace CMS\ExtranetBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Folder
*
* @ORM\Table(name="folder")
* @ORM\HasLifecycleCallbacks
* @ORM\Entity(repositoryClass="CMS\ExtranetBundle\Repository\FolderRepository")
*/
class Folder
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="guid")
* @ORM\GeneratedValue(strategy="UUID")
*/
public $id;
// Used in NotificationListener
public $beforeRemoveId;
/**
* @ORM\OneToMany(targetEntity="Document", mappedBy="folder", cascade={"persist", "remove"})
*/
public $documents;
}
Run Code Online (Sandbox Code Playgroud)
文档 :
<?php
namespace CMS\ExtranetBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Document
* @ORM\Entity(repositoryClass="CMS\ExtranetBundle\Repository\DocumentRepository")
* @ORM\Table(name="document")
* @ORM\HasLifecycleCallbacks
*/
class Document
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="guid")
* @ORM\GeneratedValue(strategy="UUID")
*/
public $id;
/**
* @ORM\ManyToOne(targetEntity="Folder", inversedBy="documents", cascade={"persist"})
* @ORM\JoinColumn(name="folder_id", referencedColumnName="id", nullable=true)
*/
public $folder;
}
Run Code Online (Sandbox Code Playgroud)
和通知:
<?php
namespace CMS\ExtranetBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="notification")
* @ORM\Entity(repositoryClass="CMS\ExtranetBundle\Repository\NotificationRepository")
* @ORM\HasLifecycleCallbacks
*/
class Notification
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="guid")
* @ORM\GeneratedValue(strategy="UUID")
*/
public $id;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="notifications", cascade={"persist"})
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
*/
public $user;
/**
* @ORM\Column(name="type", type="string", length=16)
*/
public $type;
/**
* @ORM\Column(name="object_id", type="string", length=36)
*/
public $objectId;
}
Run Code Online (Sandbox Code Playgroud)
最后,我有一个侦听器,而不是“侦听”“文档”实体(preRemove 和 postRemove)以删除通知实体。通知实体不通过关系链接到文档,因为字段“objectId”是通用的,它可以包含其他实体,具体取决于“type”属性。
这是我的听众:
<?php
namespace CMS\ExtranetBundle\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
class NotificationListener
{
/**
* Enregistre l'ID avant la suppression pour l'utiliser dans le postRemove
*
* @param LifecycleEventArgs $args
* @return bool|void
*/
public function preRemove(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$class = (new \ReflectionClass($entity))->getShortName();
if (!$this->isOfValidClass($class)) {
return;
}
$entity->beforeRemoveId = $entity->getId();
}
/**
* Lors de la suppression d'une entité, supprime les notifications correspondantes.
*
* @param LifecycleEventArgs $args
* @return bool|void
*/
public function postRemove(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$class = (new \ReflectionClass($entity))->getShortName();
if (!$this->isOfValidClass($class)) {
return;
}
$id = $entity->beforeRemoveId;
if (!$id) {
return;
}
$em = $args->getEntityManager();
$notifications = $em->getRepository('CMSExtranetBundle:Notification')->findBy([
'type' => strtolower($class),
'objectId' => $id
]);
if (count($notifications)) {
$batchSize = 20;
$i = 1;
foreach ($notifications as $notification) {
$em->remove($notification);
if (($i % $batchSize) === 0) {
$em->flush();
$em->clear();
}
++$i;
}
// It fails after calling flush()
$em->flush();
}
}
private function isOfValidClass($class)
{
$allowedClasssNames = [
'Document',
];
foreach ($allowedClasssNames as $allowedClasssName) {
if ($class == $allowedClasssName) {
return true;
}
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试删除包含多个文档的文件夹实体。
这是我的控制器:
<?php
namespace CMS\ExtranetBundle\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use CMS\ExtranetBundle\Entity\Folder;
use CMS\ExtranetBundle\Security\Voters\FolderVoter;
class FolderController extends DefaultController
{
public function deleteAction(Request $request, Folder $folder)
{
// Authorization
$this->denyAccessUnlessGranted(FolderVoter::WRITE, $folder);
$em = $this->getDoctrine()->getManager();
$em->remove($folder);
$em->flush();
return new JsonResponse([
'status' => true,
]);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,有时,当我删除一个文件夹,其中包含文档,有通知,我收到这个错误:
(NotificationListener.php 的第 73 行对应$em->flush();postRemove 中循环后的最后一个)
如果文档没有通知,则它有效
任何的想法 ?
好的,感谢 irc.freenode.net 上的 srm`@#symfony-fr,我改变了删除通知的方式。这是我的 NotificationListener 的 postRemove() 方法:
\n\n/**\n * Lors de la suppression d'une entit\xc3\xa9, supprime les notifications correspondantes.\n *\n * @param LifecycleEventArgs $args\n * @return bool|void\n */\npublic function postRemove(LifecycleEventArgs $args)\n{\n $entity = $args->getEntity();\n\n // R\xc3\xa9cup\xc3\xa8re le nom 'court' de la classe\n $class = (new \\ReflectionClass($entity))->getShortName();\n\n if (!$this->isValidClass($class)) {\n return;\n }\n $id = $entity->beforeRemoveId;\n if (!$id) {\n return;\n }\n $em = $args->getEntityManager();\n\n $queryBuilder = $em\n ->createQueryBuilder()\n ->delete('CMSExtranetBundle:Notification', 'n')\n ->where('n.type = :type')\n ->andWhere('n.objectId = :objectIds')\n ->setParameter(':type', strtolower($class))\n ->setParameter(':objectIds', $entity->beforeRemoveId);\n\n $queryBuilder->getQuery()->execute();\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n谢谢 !:-D
\n