- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 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;
}
<?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;
}
<?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;
}
<?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;
}
}
<?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,
]);
}
}
$em->flush();
)
最佳答案
好的,感谢 irc.freenode.net 上的 srm`@#symfony-fr,我改变了删除通知的方式。这是我的 NotificationListener 的 postRemove() 方法:
/**
* 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();
// Récupère le nom 'court' de la classe
$class = (new \ReflectionClass($entity))->getShortName();
if (!$this->isValidClass($class)) {
return;
}
$id = $entity->beforeRemoveId;
if (!$id) {
return;
}
$em = $args->getEntityManager();
$queryBuilder = $em
->createQueryBuilder()
->delete('CMSExtranetBundle:Notification', 'n')
->where('n.type = :type')
->andWhere('n.objectId = :objectIds')
->setParameter(':type', strtolower($class))
->setParameter(':objectIds', $entity->beforeRemoveId);
$queryBuilder->getQuery()->execute();
}
关于php - Symfony 与教义 : UnitOfWork undefined index after calling flush(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44945366/
我关注了this tutorial . 我已经到了使用 _unitOfWork.XYZRepository.Get() 调用存储库的阶段,现在更进一步,我想为我的 UnitOfWork 类编写一个接口
我的业务对象存储在两个数据存储中。对象的一部分存储在 Azure 表存储中,另一部分存储在 Azure SQL 中。基本上 SQL 部分用于查询,而表存储用于占用大量空间的属性。 大多数时候,只使用对
显然(而且很有可能)我当前的 UnitOfWork 实现存在缺陷,因为我在同时进行多个调用时出现连接错误。 异常: The underlying provider failed on Open. 内部
我目前已经做了一个 UnitOfWork 实现,它包装了数据库连接和事务。 using (var uow = UnitOfWorkFactory.Create()) { // do db op
在 DDD 模式中,工作单元应该与存储库耦合吗?我见过几个不同的例子,包括一个实现工作单元接口(interface)的存储库,一个实现工作单元本身行为的存储库,以及一个具有代表工作单元的属性的存储库,
我有一个像这样的特定和通用存储库: 通用存储库: public class Repository : IRepository where TEntity : class { protected
我正在使用以下 UnitOfWork 类,以便通过使用泛型为我的实体获取存储库。 public class UnitOfWork : IUnitOfWork { private readonly
我认为这是一个常见问题,但经过一番搜索后我找不到任何相关内容。 我遇到的问题是,当使用 @UnitOfWork 注释我的资源方法并在我的资源方法内部时,我得到了一个 No Hibernate Sess
我正在为我们需要构建的小型应用构建单元测试。 我已经实现了存储库/工作单元模式。我的经理类实现了工作单元模式。 对于给定的接口(interface): public interface IUserMa
对 FluentNHibernate 很陌生,但我也对这个领域感到兴奋。我最近开始使用上述方法开发新的 DAL,并且一直在阅读存储库模式。我喜欢此模式采用的通用形式,并希望将此模式与 UnitOfWo
我有以下类(class): public FooDAO extends AbstractDAO { // Dropwizard DAO @Inject FooDAO(SessionFactory
我有这些类(class): public static class UnitOfWorkSS { public static IUnitOfWork Begin() { return
我正在尝试实现一个通用的存储库模式。我找到了这个网站,我认为它解释得很好。 http://www.tugberkugurlu.com/archive/generic-repository-patter
如果我实现一些简单的 OR/M 工具,我应该把恒等映射放在哪里?显然,每个 Repository 都应该有权访问自己的标识映射,以便它可以注册加载的对象(或者 DataMapper 是在 Identi
互联网上到处都是关于UnitOfWork的信息图案;甚至 SO 也不异常(exception)。 我还是不明白。据我了解 UnitOfWork = Transaction in DB .仅此而已;不多
我已经实现了我的 UnitOfWork,以便它保留对所有存储库的引用。 public interface IUnitOfWork { void Commit(); void RollBac
当使用 Rhino Commons UnitOfWork(在 ASP-MVC 的 UnitOfWorkApplication 中)时,我喜欢使用 Rhino Repository 静态类来保存这样的实
我有一个 Doctrine 事件监听器,用于监听 onFlush 事件。我使用它在保存时更新实体上的电子标签。 我需要访问计划删除的实体,以便我可以访问它们的关联对象,但是: 我使用的是软删除过滤器,
我在网上看到了很多关于 UnitOfWork 和 Repo 模式的信息,但仍然不清楚为什么以及何时使用——这让我有些困惑。 考虑到我可以通过使用 DI 通过使用 IoC 来测试我的存储库,如本文 Wh
我在后台任务方法(由 Quartz 操作)中使用 UnitOfWork,在 hibernate 之上使用 Guice-persist。后台任务调用一个服务,它需要在它的任务中间提交当前事务——然后继续
我是一名优秀的程序员,十分优秀!