我有一段重复的代码,将在我的 Symfony2 项目中的多个实体中使用,因此应用某种 DRY 就可以了,当然如果可能的话,我正在考虑 PHP Traits .
private static $preDeletedEntities;// static array that will contain entities due to deletion.
private static $deletedEntities;// static array that will contain entities that were deleted (well, at least the SQL was thrown).
/**
* This callback will be called on the preRemove event
* @ORM\PreRemove
*/
public function entityDueToDeletion()
{
// This entity is due to be deleted though not deleted yet.
self::$preDeletedEntities[] = $this->getId();
}
/**
* This callback will be called in the postRemove event
* @ORM\PostRemove
*/
public function entityDeleted()
{
// The SQL to delete the entity has been issued. Could fail and trigger the rollback in which case the id doesn't get stored in the array.
self::$deletedEntities[] = $this->getId();
}
public static function getDeletedEntities()
{
return array_slice(self::$preDeletedEntities, 0, count(self::$deletedEntities));
}
public static function getNotDeletedEntities()
{
return array_slice(self::$preDeletedEntities, count(self::$deletedEntities)+1, count(self::$preDeletedEntities));
}
public static function getFailedToDeleteEntity()
{
if(count(self::$preDeletedEntities) == count(self::$deletedEntities)) {
return NULL; // Everything went ok
}
return self::$preDeletedEntities[count(self::$deletedEntities)]; // We return the id of the entity that failed.
}
public static function prepareArrays()
{
self::$preDeletedEntities = array();
self::$deletedEntities = array();
}
这是我想到的代码:
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\HasLifecycleCallbacks()
*/
trait DeleteLifeCycleCallbacksTrait
{
// write things here
}
但是注释会应用于实体吗?可以吗?你会怎样做才能避免不重复代码?
编辑:尝试找到最佳方法
从 @Cerad 用户那里得到一些想法,并且正如文档所说,生命周期事件监听器比简单的生命周期回调功能强大得多,然后我将开始使用它们。
因此,首先,这个生命周期回调|监听器|订阅者的目的是存储每个持久对象的 ID,以便我可以以某种方式获取它并从 Controller 发送回 View 。作为一个简单的视觉示例,假设我从 View 向 Controller 发送这个值数组 (1, 2, 3, 4, 5)
,由于某些 X 原因,只有 1 ,4 和 5持久化(意味着从数据库中完全删除)到数据库,对吗?
我们还要说,我将在 Producto
实体中使用事件监听器。因此,如果不进行测试而仅从示例中获取代码,则 Listener
的代码应该如下所示:
use Doctrine\ORM\Event\LifecycleEventArgs;
use Entity\Producto;
class StoreDeletedIds
{
private $deletedItems = [];
public function postDelete(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$entityManager = $args->getEntityManager();
if ($entity instanceof Producto) {
array_push($deletedItems, $entity->getId());
}
}
}
我对此的疑问是:
- 上面的代码可以吗?
- 每次 Doctrine 调用监听器时,
$deletedItems
都会被清理吗? - 如何返回
$deletedItems
以便在 Controller 上捕获它并将其发送回 View ? - 我还需要定义订阅者吗?为什么?
这对我来说是新主题,所以我需要一些建议
最佳答案
在@PeterPopelyshko评论之后,这是我提供的解决方案,只需定义一个抽象类Model\DeleteLifeCycleCallbacks.php
并将代码放入其中:
use Doctrine\ORM\Mapping as ORM; // not so sure if this is need here
abstract class DeleteLifeCycleCallbacks
{
private static $preDeletedEntities;// static array that will contain entities due to deletion.
private static $deletedEntities;// static array that will contain entities that were deleted (well, at least the SQL was thrown).
/**
* This callback will be called on the preRemove event
* @ORM\PreRemove
*/
public function entityDueToDeletion()
{
// This entity is due to be deleted though not deleted yet.
self::$preDeletedEntities[] = $this->getId();
}
/**
* This callback will be called in the postRemove event
* @ORM\PostRemove
*/
public function entityDeleted()
{
// The SQL to delete the entity has been issued. Could fail and trigger the rollback in which case the id doesn't get stored in the array.
self::$deletedEntities[] = $this->getId();
}
public static function getDeletedEntities()
{
return array_slice(self::$preDeletedEntities, 0, count(self::$deletedEntities));
}
public static function getNotDeletedEntities()
{
return array_slice(self::$preDeletedEntities, count(self::$deletedEntities)+1, count(self::$preDeletedEntities));
}
public static function getFailedToDeleteEntity()
{
if(count(self::$preDeletedEntities) == count(self::$deletedEntities)) {
return NULL; // Everything went ok
}
return self::$preDeletedEntities[count(self::$deletedEntities)]; // We return the id of the entity that failed.
}
public static function prepareArrays()
{
self::$preDeletedEntities = array();
self::$deletedEntities = array();
}
}
然后按如下方式使用:
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
*/
class Producto extends Model\DeleteLifeCycleCallbacks
{
// entity methods and properties here
}
关于php - 干 : how to use this code in several entities accross Symfony2 project? 特征?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28109912/