如何在phalcon下新建數據庫表,並解決字段TIMESTAMP的BUG

前言:我的phalcon版本3.3.2

1.創建一個數據庫服務(store_log_db)

$di->setShared('store_log_db', function () {
    $config = $this->getConfig();

    $class = 'Phalcon\Db\Adapter\Pdo\\' . $config->database->adapter;
    $params = [
        'host'     => $config->store_log_database->host,
        'username' => $config->store_log_database->username,
        'password' => $config->store_log_database->password,
        'dbname'   => $config->store_log_database->dbname,
        'charset'  => $config->store_log_database->charset
    ];

    if ($config->database->adapter == 'Postgresql') {
        unset($params['charset']);
    }

    $connection = new $class($params);

    return $connection;
});

2.創建數據庫表的代碼

$connection = $this->getDI()->get('store_log_db');
if (!$connection->tableExists($tableName)) {
                    $connection->createTable($tableName, null,
                        array(
                            "indexes" => array(
                                new Index("mobile", ['mobile']),
                                new Index('method', ['method'])
                            ),
                            "options" => array(
                                "TABLE_TYPE"      => "BASE TABLE",
                                "ENGINE"          => "InnoDB",
                                "TABLE_COLLATION" => "utf8mb4_unicode_ci",
                            ),
                            "columns" => array(
                                new Column(
                                    "id",
                                    array(
                                        "type" => Column::TYPE_INTEGER,
                                        "size" => 10,
                                        'unsigned' => true,
                                        "notNull" => true,
                                        "autoIncrement" => true,
                                        "primary" => true,
                                        'first'         => true,
                                    )
                                ),
                                new Column(
                                    "order_id",
                                    array(
                                        "type" => Column::TYPE_INTEGER,
                                        "size" => 10,
                                        "unsigned" => true,
                                        'default' => 0,
                                        'after'    => 'id',
                                    )
                                ),
                                new Column(
                                    "type",
                                    array(
                                        "type" => Column::TYPE_VARCHAR,
                                        "size" => 64,
                                        'after'    => 'order_id',
                                    )
                                ),
                                new Column(
                                    "method",
                                    array(
                                        "type" => Column::TYPE_VARCHAR,
                                        "size" => 32,
                                        'after'    => 'type',
                                    )
                                ),
                                new Column(
                                    "mobile",
                                    array(
                                        "type" => Column::TYPE_VARCHAR,
                                        "size" => 11,
                                        'after'    => 'method',
                                    )
                                ),
                                new Column(
                                    "authorization",
                                    array(
                                        "type" => Column::TYPE_VARCHAR,
                                        "size" => 20,
                                        'after'    => 'mobile',
                                    )
                                ),
                                new Column(
                                    "request_data",
                                    array(
                                        "type" => Column::TYPE_TEXT,
                                        'after'    => 'authorization',
                                    )
                                ),
                                new Column(
                                    "response_data",
                                    array(
                                        "type" => Column::TYPE_TEXT,
                                        'after'    => 'request_data',
                                    )
                                ),
                                new Column(
                                    'updated_at',
                                    array(
                                        'type'          => 'TIMESTAMP',
                                        'typeReference' => Column::TYPE_TIMESTAMP,
                                        'default'       => 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',
                                        "notNull" => false,
                                        'after'         => 'response_data',
                                    )
                                ),
                                new Column(
                                    "created_at",
                                    array(
                                        'type'          => 'TIMESTAMP',
                                        'typeReference' => Column::TYPE_TIMESTAMP,
                                        'default'       => 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',
                                        "notNull" => false,
                                        'after'    => 'updated_at',
                                    )
                                ),
                            )
                        )
                    );

3.創建timestamp字段可能會出現的問題

此錯誤問題的git解決方案

4.參考文獻

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章