简体   繁体   English

Symfony2:哪里有slug和timestamp方法?

[英]Symfony2: Where place slug and timestamp methods?

I am coding a service which will handle articles (CRUD). 我正在编写一个处理文章的服务(CRUD)。

The persistence layer is handles by an ArticleManager >which does Repository and CRUD actions. 持久层由ArticleManager>处理,它执行Repository和CRUD操作。

Now I want to implement two attributes: createdAt and >updatedAt 现在我想实现两个属性:createdAt和> updatedAt

My question is now where I should place them: In the entity, in the ArticleManager, somewhere else? 我现在的问题是我应该放置它们:在实体中,在ArticleManager中,在其他地方?

Best Regards, Bodo 最诚挚的问候,Bodo

Ah, 啊,

I see, the FOSUserBundle handles this task with an EventListener: 我看,FOSUserBundle使用EventListener处理此任务:

https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Entity/UserListener.php https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Entity/UserListener.php

But thank you for youre help :) 但是谢谢你的帮助:)

<?php

namespace LOC\ArticleBundle\Entity;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Events;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use LOC\ArticleBundle\Model\ArticleInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;


class ArticleListener implements EventSubscriber
{
private $articleManager;
private $container;

public function __construct(ContainerInterface $container)
{
    $this->container = $container;
}

public function getSubscribedEvents()
{
    return array(
        Events::prePersist,
        Events::preUpdate,
    );
}

public function prePersist(LifecycleEventArgs $args)
{
    $article = $args->getEntity();

    $article->setCreatedAt(new \DateTime());

    $this->articleManager->updateArticle($article);
}

public function preUpdate(PreUpdateEventArgs $args)
{
    $article = $args->getEntity();

    $article->setUpdatedAt(new \DateTime());

    $this->articleManager->updateArticle($article);
}
}

Well, there is a bundle for such stuff, the DoctrineExtensionsBundle . 好吧,有一个包这样的东西, DoctrineExtensionsBundle It got Timestampable and slugable. 它有Timestampable和slugable。

If you want to do it on your own, the place is definitly in the Entity itself, as you don't want to mess around in your controller. 如果你想自己做,这个地方肯定在实体本身,因为你不想在你的控制器中乱七八糟。 Here is how I do the Timestampable as I don't use the DoctrineExtensionsBundle: 以下是我如何使用Timestampable,因为我不使用DoctrineExtensionsBundle:

/**
 * @ORM\Entity
 * @ORM\Table(name="entity")
 * @ORM\HasLifecycleCallbacks
 */
class Entity {
    // ...

    /**
     * @ORM\Column(name="created_at", type="datetime", nullable=false)
     */
    protected $createdAt;

    /**
     * @ORM\Column(name="updated_at", type="datetime", nullable=false)
     */
    protected $updatedAt;

    /**
     * @ORM\prePersist
     */
    public function prePersist() {
        $this->createdAt = new \DateTime();
        $this->updatedAt = new \DateTime();
    }

    /**
     * @ORM\preUpdate
     */
    public function preUpdate() {
        $this->updatedAt = new \DateTime();
    }

    // ...

}

As for my decision not to use the Bundle: When symfony2 was released as stable, this bundle didn't exist (or it wasn't stable, I don't remember) so I started doing it on my own like shown below. 至于我决定不使用Bundle:当symfony2被释放为稳定时,这个包不存在(或者它不稳定,我不记得)所以我开始自己做这个如下所示。 As it is little overhead, I kept doing it like this and never felt the need to change it. 由于它的开销很小,我一直这样做,从来没有觉得需要改变它。 If you need Slugable or want to keep it simply, try the bundle! 如果您需要Slugable或想要保持简单,请尝试捆绑!

在实体中,因为它是逻辑上所属的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM