4
4

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.

doctrineでidが生成されるタイミング

Posted at

PostgreSQLとMySqlで、persistした後のidが取得できるタイミングが違ってたのでメモ。

サンプル

Order.php

/**
 * Order
 */
class Order extends \Eccube\Entity\AbstractEntity
{
    /**
     * @var integer
     */
    public $order_id;

    /**
     * @var integer
     */
    public $total;
...

order.dcm.yml

Eccube\Entity\Order:
    type: entity
    table: dtb_order
    id:
        order_id:
            type: integer
            nullable: false
            unsigned: false
            id: true
            generator:
                strategy: AUTO
    fields:
        total:
            type: integer
            nullable: true

テストコード


$Order = new Order();
$Order->setTotal(100);

$app['orm.em']->persist($Order);
$id1 = $Order->getOrderId();

var_dump($id1);

$app['orm.em']->flush();
$id2 = $Order->getOrderId();

var_dump($id2);

Mysqlの場合

NULL
int(1)

PostgreSQLの場合

int(1)
int(1)

のようになりました。

PostgreSQLだとpersistのタイミングでIDを取得できるようです。
シーケンスとオートインクリメントの違いかな。

4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?