DRY: how to use this code in several entities accross Symfony2 project? Traits?

别说谁变了你拦得住时间么 提交于 2019-12-06 04:24:05

Following @PeterPopelyshko comment this is the solution I come with, just define a abstract class Model\DeleteLifeCycleCallbacks.php and put the code inside:

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();
    }
}

Then use it as follow:

/**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks()
 */
class Producto extends Model\DeleteLifeCycleCallbacks
{
    // entity methods and properties here
}
Marcel Burkhard

Don't

Business logic doesn't belog into entities.

Traits won't help you either, because while duplicating code is a bad thing, abusing traits is imo even worse.

I once wondered wether I should use traits and posted a question on codereview (https://codereview.stackexchange.com/a/74195/56686). I have yet to stumble upon a valid use case for traits in my applications.

Make a service

I advice you to make a service and put your logic there. Documentation: http://symfony.com/doc/current/book/service_container.html

Because you can't call symfony services from doctrine lifecyclecallbacks, you would have to drop those.

You might wan't to fire up your searchengine of choice to look for a step-by-step tutorial.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!