Skip to main content

การทำให้ Laravel รองรับ Primary Key แบบหลาย Columns (Composite Keys)

ปัจจุบัน Eloquent ของ Laravel Framework ยังไม่รองรับการบันทึกข้อมูลที่มี Primary Key แบบหลาย Columns ได้ ผมได้ค้นหาจากอินเตอร์เน็ต และพบว่ามีคนที่แนะนำแนวทางแก้ไข โดยใช้วิธีการ Overide method save ของ Eloquent ขึ้นมาแทน ซึ่งผมได้ทดลองใช้แล้วสามารถทำงานได้อย่างถูกต้อง จึงขอแนะนำการนำ Method save ที่ถูกปรับปรุงมาเขียนเป็น Base Class ของ Model ดังตัวอย่าง


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use DB;

class BaseModel extends Model
{

    public function save(array $options = []) {
        if( ! is_array($this->getKeyName()))
        {
            return parent::save($options);
        }

        // Fire Event for others to hook
        if($this->fireModelEvent('saving') === false) return false;

        // Prepare query for inserting or updating
        $query = $this->newQueryWithoutScopes();

        // Perform Update
        if ($this->exists)
        {
            if (count($this->getDirty()) > 0)
            {
                // Fire Event for others to hook
                if ($this->fireModelEvent('updating') === false)
                {
                    return false;
                }

                // Touch the timestamps
                if ($this->timestamps)
                {
                    $this->updateTimestamps();
                }

                //
                // START FIX
                //


                // Convert primary key into an array if it's a single value
                $primary = (count($this->getKeyName()) > 1) ? $this->getKeyName() : [$this->getKeyName()];

                // Fetch the primary key(s) values before any changes
                $unique = array_intersect_key($this->original, array_flip($primary));

                // Fetch the primary key(s) values after any changes
                $unique = !empty($unique) ? $unique : array_intersect_key($this->getAttributes(), array_flip($primary));

                // Fetch the element of the array if the array contains only a single element
                //$unique = (count($unique) <> 1) ? $unique : reset($unique);

                // Apply SQL logic
                $query->where($unique);

                //
                // END FIX
                //

                // Update the records
                $query->update($this->getDirty());

                // Fire an event for hooking into
                $this->fireModelEvent('updated', false);
            }
        }
        // Insert
        else
        {
            // Fire an event for hooking into
            if ($this->fireModelEvent('creating') === false) return false;

            // Touch the timestamps
            if($this->timestamps)
            {
                $this->updateTimestamps();
            }

            // Retrieve the attributes
            $attributes = $this->attributes;

            if ($this->incrementing && !is_array($this->getKeyName()))
            {
                $this->insertAndSetId($query, $attributes);
            }
            else
            {
                $query->insert($attributes);
            }

            // Set exists to true in case someone tries to update it during an event
            $this->exists = true;

            // Fire an event for hooking into
            $this->fireModelEvent('created', false);
        }

        // Fires an event
        $this->fireModelEvent('saved', false);

        // Sync
        $this->original = $this->attributes;

        // Touches all relations
        if (array_get($options, 'touch', true)) $this->touchOwners();

        return true;
    }    
}

โดยการนำเอา BaseModel นี้ไปใช้ในรูปแบบการ Extend Model Class ดังต่อไปนี้


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\BaseModel;

class MyTable extends BaseModel
{

    protected $table = 'MyTable';
    protected $primaryKey = ['MY_KEY_COLUMN_1', 'MY_KEY_COLUMN_2', 'MY_KEY_COLUMN_3'];
    
    public $incrementing = false;
    public $timestamps = false;

}


และสามารถเรียกใช้งานได้เป็นปกติได้เลย เช่น


1
2
3
4
5
6
7
$myTable = new MyTable;
$myTable->FIELD1 = 'Feild1_Value';
$myTable->FIELD2 = 'Field2_Value';
$myTable->MY_KEY_COLUMN1 = 'Key1_Value';
$myTable->MY_KEY_COLUMN2 = 'Key2_Value';
$myTable->MY_KEY_COLUMN3 = 'Key3_Value';
$myTable->save();

Credits

- ขอบคุณข้อมูล Method Save จากคคุณ tsilenzio ซึ่งได้ post ไว้ใน https://github.com/laravel/framework/issues/5517


Comments

Popular posts from this blog

Serialize and Deserialize JSON data with C#

This post is about example for serialize and deserialize json data with C#. This example using Newtonsoft.Json to be a library to work with json data. You can add Newtonsoft.Json by download from  https://www.newtonsoft.com/json  or using Nuget to add it into your project. In this example is Bill object that hold billing data about Car object. Serialize Object to String Below is example code to serialize object to json string. At line 39 is code to convert object into json string with beautiful json format. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; namespace JSON { class JSonExample { static void Main( string [] args) { List<Car> cars = new List<Car>(); float to...

How to clear Oracle Alert Log and Trace files using ADRCI command?

Sometime you will face the problem about large alert log and trace files in Oracle database. You can use ADRCI tool to manage them. Run adrci command [oracle@dboraclel1 dboraclel1]$ adrci ADRCI: Release 12.2.0.1.0 - Production on Tue Aug 21 00:20:25 2018 Copyright (c) 1982, 2017, Oracle and/or its affiliates. All rights reserved. ADR base = "/u01/app/oracle" adrci> show home ADR Homes: diag/rdbms/schooldb/schooldb diag/tnslsnr/dboraclel1/listener diag/tnslsnr/dboraclel1/listener6 diag/tnslsnr/dboraclel1/listener2 diag/tnslsnr/dboraclel1/listener3 diag/tnslsnr/dboraclel1/listener4 diag/tnslsnr/dboraclel1/listener5 diag/diagtool/user_oracle/adrci_3644429138_107 adrci> Set home directory to work. If you have only one home folder skip this step. set homepath diag/tnslsnr/dboraclel1/listener Run this command to purge alert log. PURGE -age 1440 -type ALERT This command will be purge alert log older than 1440 minutes. Parameter age i...

Laravel 5.6.34 Released

We’ve had an exciting week in Laravel-land, with the launch of Laravel Nova on Wednesday! Tuesday’s release of Laravel 5.6.34 released quietly before Nova, bringing a few changes and a URL validation compatibility fix for PHP 7.3. First up, the whereRowValues changes to properly wrap columns: $builder = DB::table('table')->whereRowValues(['column1', 'column2'], '=', ['foo', 'bar']); dd($builder->toSql()); // expected: select * from "table" where ("column1", "column2") = (?, ?) // actual: select * from "table" where (column1, column2) = (?, ?) Next, the default mail template’s copyright phrase (i.e., “All rights reserved.”) is localizable. Here’s the implementation: @lang('All rights reserved.') The last change was updating the behavior of the EventFake to dispatch non-faked events. The previous functionality was not to execute any event listeners. Now, al...