Welcome Guest, Not a member yet? Register   Sign In
uuid
#1

hii, how to set uuid as primary key in ci4 ??
database used mysql / postgresql
Reply
#2

What database? What model? What code are you trying?
Reply
#3

This may help you understand how MySQL uses the UUID.

MySQLTutorial -> MySQL UUID Smackdown: UUID vs. INT for Primary Key
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(05-11-2023, 10:19 PM)InsiteFX Wrote: This may help you understand how MySQL uses the UUID.

MySQLTutorial -> MySQL UUID Smackdown: UUID vs. INT for Primary Key

oke thank you for the info, but what is the best pratic if i want to integrated uuid as primary key in my database/ model
should i custome in callback model before insert?
Reply
#5

(This post was last modified: 05-11-2023, 10:54 PM by InsiteFX.)

NOTE: That the MySQL UUID only works with MySQL 8+

For any other version you will need to roll your own methods to handle UUID.

Ok, I found this one on MySQL 5.7 in their Blog Archive.

MySQL Blog Archive -> Storing UUID Values in MySQL Tables
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(05-11-2023, 10:46 PM)InsiteFX Wrote: NOTE: That the MySQL UUID only works with MySQL 8+

For any other version you will need to roll your own methods to handle UUID.

Ok, I found this one on MySQL 5.7 in their Blog Archive.

MySQL Blog Archive -> Storing UUID Values in MySQL Tables

oke thank you
Reply
#7

(This post was last modified: 05-13-2023, 04:25 AM by gosocial2. Edit Reason: Added forgotten )

Here's an implementation (also used by CodeIgniter Wizard):

In the database table set the PK field as char(36).

E.g. let's assume we have a table named cms_pages whose primary key is a field named uuid and is of type char(36)

Create a trait for UUID generation:


PHP Code:
<?php

namespace App\Models\Traits;

trait 
HasUuid
{
    public function setNewUUID(array $data) {

        if (! isset($data['data']['uuid']) || empty($data['data']['uuid'])) {
            $data['data']['uuid'] = newUUID();
        }

        return $data;

    }
}

?>


Example model using the trait:


PHP Code:
<?php
namespace App\Models\Admin;

use 
App\Models\Traits\HasUuid;
use 
App\Models\Traits\HasSlug;

class 
PageModel extends \App\Models\GoBaseModel
{
    use HasUuidHasSlug;

    protected $table "cms_pages"// remember to scroll down to see the rest of the code...

    /**
    * Whether primary key uses auto increment.
    *
    * @var bool
    */
    protected $useAutoIncrement false;

    protected $primaryKey "uuid";
    
    
protected $beforeInsert = ["setNewUUID""setSlug"];

    protected $allowedFields = [
        "uuid"// <- this field is the primary key field of type char(36) in the database
        "legacy_id"// rename your existing, non-UUID PK implementation if any
        "parent_uuid"// optional candidate for FK to itself - useful for CRUD generators
        "primary_category_uuid", // optional candidate for FK to itself - useful for CRUD generators
        "primary_language_code",
        "title",
        "content", 
         
// so on...
    ]
// and so forth...
?>

In a custom helper function file (e.g. go_helper.php):

PHP Code:
if (!function_exists('newUUID')) {

    function newUUID() {

        if (function_exists('com_create_guid') === true) {

            return trim(com_create_guid(), '{}');

        }



        return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X'mt_rand(065535), mt_rand(065535), mt_rand(065535), mt_rand(1638420479), mt_rand(3276849151), mt_rand(065535), mt_rand(065535), mt_rand(065535));

    }




CodeIgniter Wizard (CRUD code generator for Mac) instantly scaffolds Bootstrap-based web applications with an administrative interface (admin templates include Bootstrap5)

Reply
#8

See https://github.com/michalsn/codeigniter4-uuid
Reply




Theme © iAndrew 2016 - Forum software by © MyBB