2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

create_date/update_date

Posted at

はじめに

create_date/update_date列に自動で値が入るのはどこでやっているのか

setCreateDate/setUpdateDate

Insert

ここでやっているっぽい。

src/Eccube/Doctrine/EventSubscriber/SaveEventSubscriber.php
/**
 * @param LifecycleEventArgs $args
 */
public function prePersist(LifecycleEventArgs $args)
{
    $entity = $args->getObject();

    if (method_exists($entity, 'setCreateDate')) {
        $entity->setCreateDate(new \DateTime());
    }
    if (method_exists($entity, 'setUpdateDate')) {
        $entity->setUpdateDate(new \DateTime());
    }
    if (method_exists($entity, 'setCurrencyCode')) {
        $currency = $this->eccubeConfig->get('currency');
        $entity->setCurrencyCode($currency);
    }
    if (method_exists($entity, 'setCreator')) {
        $user = $this->requestContext->getCurrentUser();
        if ($user instanceof Member) {
            $entity->setCreator($user);
        }
    }
}

EntityにsetCreateDate()/setUpdateDate()というメソッドがあれば設定されるっぽい。

Update

ここでやっているっぽい。

src/Eccube/Doctrine/EventSubscriber/SaveEventSubscriber.php
/**
 * @param LifecycleEventArgs $args
 */
public function preUpdate(LifecycleEventArgs $args)
{
    $entity = $args->getObject();

    if (method_exists($entity, 'setUpdateDate')) {
        $entity->setUpdateDate(new \DateTime());
    }

    if (method_exists($entity, 'setCreator')) {
        $user = $this->requestContext->getCurrentUser();
        if ($user instanceof Member) {
            $entity->setCreator($user);
        }
    }
}

EntityにsetUpdateDate()というメソッドがあれば設定されるっぽい。

まとめ

メソッドさえあれば良さ気なので列名は何でも良いのかもしれない。

あとUTCで登録されるようなのでphpMyAdminなどで見ると一瞬-9hで表示されて焦る。
取得時にTimezoneに合わせて変換するようだ。

参照

処理中に逐一DBに保存したい
タイムゾーン 日時データの保存と表示

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?