{"id":14987330,"url":"https://github.com/michalsn/codeigniter4-uuid","last_synced_at":"2025-08-21T20:32:31.527Z","repository":{"id":51704740,"uuid":"259223364","full_name":"michalsn/codeigniter4-uuid","owner":"michalsn","description":"UUID package for CodeIgniter 4 with support for Model and Entity.","archived":false,"fork":false,"pushed_at":"2024-04-22T14:19:15.000Z","size":122,"stargazers_count":41,"open_issues_count":0,"forks_count":9,"subscribers_count":6,"default_branch":"develop","last_synced_at":"2024-12-08T02:42:07.871Z","etag":null,"topics":["codeigniter4","entity","model","uuid"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/michalsn.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-27T06:18:19.000Z","updated_at":"2024-10-27T00:18:57.000Z","dependencies_parsed_at":"2024-06-18T20:06:50.865Z","dependency_job_id":"393d2dec-d90c-48da-babe-e4d278809c62","html_url":"https://github.com/michalsn/codeigniter4-uuid","commit_stats":{"total_commits":78,"total_committers":4,"mean_commits":19.5,"dds":0.08974358974358976,"last_synced_commit":"bfb0376d7622d46f4e91f03231b1eb8d056c6b4d"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michalsn%2Fcodeigniter4-uuid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michalsn%2Fcodeigniter4-uuid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michalsn%2Fcodeigniter4-uuid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michalsn%2Fcodeigniter4-uuid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/michalsn","download_url":"https://codeload.github.com/michalsn/codeigniter4-uuid/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230532448,"owners_count":18240792,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codeigniter4","entity","model","uuid"],"created_at":"2024-09-24T14:14:27.607Z","updated_at":"2025-08-21T20:32:31.509Z","avatar_url":"https://github.com/michalsn.png","language":"PHP","readme":"# CodeIgniter 4 UUID [![PHP Tests](https://github.com/michalsn/codeigniter4-uuid/actions/workflows/php.yml/badge.svg)](https://github.com/michalsn/codeigniter4-uuid/actions/workflows/php.yml)\nThis package make it easy to work with UUIDs in Codeigniter 4. It provide four classes to make that possible: `Uuid`, `UuidModel`, `UuidEntity` and `UuidCast`. This implementation is tighly coupled with [Ramsey\\Uuid](https://github.com/ramsey/uuid).\n\n## Installation via composer\n\n    composer require michalsn/codeigniter4-uuid\n\n## Manual installation\n\nDownload this repo and then enable it by editing **app/Config/Autoload.php** and adding the **Michalsn\\UuidModel**\nnamespace to the **$psr4** array. For example, if you copied it into **app/ThirdParty**:\n\n```php\n\u003c?php\n\n$psr4 = [\n    'Config'      =\u003e APPPATH . 'Config',\n    APP_NAMESPACE =\u003e APPPATH,\n    'App'         =\u003e APPPATH,\n    'Michalsn\\Uuid' =\u003e APPPATH . 'ThirdParty/codeigniter4-uuid/src',\n];\n```\n\n## Versions\n\n| CodeIgniter version | This package version |\n|---------------------|----------------------|\n| `\u003e= 4.5`            | `\u003e= 1.1`             |\n| `\u003c 4.5`             | `\u003c 1.1`              |\n\n## How to use it\n\nIn general, using `UuidModel` and `UuidEntity` is no much different than using the original classes provided with CodeIgniter 4 framework. We just have some additional config options. There is a good chance that you will not need to use `Uuid` class at all, because most of the things that happens are already automated.\n\n### Uuid\n\nWorking with `Uuid` class is really simple:\n\n```php\n\u003c?php\n\n$uuid = service('uuid');\n// will prepare UUID4 object\n$uuid4 = $uuid-\u003euuid4();\n// will assign UUID4 as string\n$string = $uuid4-\u003etoString();\n// will assign UUID4 as byte string\n$byte_string = $uuid4-\u003egetBytes();\n```\n\nIf you have any additional configuration options to set to a specific UUID version then you can do it via config file.\n\n### UuidModel\n\nUUID fields are always returned as a `string` even if we store them in byte format in the database. This decision was made because of the convenience of use. We don't have to worry about field type or conversion of the data.\n\nParameter | Default value | Description\n--------- | ------------- | -----------\n`$uuidVersion` | `uuid4` | Defines the UUID version to use.\n`$uuidUseBytes` | `true` | Defines if the UUID should be stored in byte format in the database. This is recommended since will allow us to save over half the space. Also, it's quite easy to use, because we always translate UUID to a string form when retrieving the data or to a byte form when we are saving it.\n`$uuidFields` | `['id']` | Defines the fields that will be treated as UUID. By default we assume it will be a primary key, but it can be any field or fields you want.\n\nNow, let's see a simple example, how to use `UuidModel` in your code. In example below, there are no additional changes except that our model extends `UuidModel`. The primary key will be stored as UUID4 in the byte format in the database.\n\n```php\n\u003c?php\n\nnamespace App\\Models;\n\nuse Michalsn\\Uuid\\UuidModel;\n\nclass Project1Model extends UuidModel\n{\n    protected $table      = 'projects_1';\n    protected $primaryKey = 'id';\n\n    protected $returnType = 'array';\n    protected $useSoftDeletes = true;\n\n    protected $allowedFields = ['name', 'description', 'created_at', 'updated_at', 'deleted_at'];\n\n    protected $useTimestamps = true;\n\n    protected $validationRules = [\n        'name' =\u003e 'required|min_length[3]',\n        'description' =\u003e 'required',\n    ];\n}\n\n```\n\nNow, here is an example where we will use the UUID but not as a primary key.\n\n```php\n\u003c?php\n\nnamespace App\\Models;\n\nuse Michalsn\\Uuid\\UuidModel;\n\nclass Project2Model extends UuidModel\n{\n    protected $uuidFields = ['category_id'];\n\n    protected $table      = 'projects_2';\n    protected $primaryKey = 'id';\n\n    protected $returnType = 'array';\n    protected $useSoftDeletes = true;\n\n    protected $allowedFields = ['category_id', 'name', 'description', 'created_at', 'updated_at', 'deleted_at'];\n\n    protected $useTimestamps = true;\n\n    protected $validationRules = [\n        'category_id' =\u003e 'required',\n        'name' =\u003e 'required|min_length[3]',\n        'description' =\u003e 'required',\n    ];\n}\n\n```\n\n### UuidEntity\n\nUsing the `UuidEntity` is only required if we store UUID fields in the byte format. In other case there are no benefits over original `Entity` class. The same as in the `UuidModel`, by default we assume that only primary key will have the UUID type.\n\nParameter | Default value | Description\n--------- | ------------- | -----------\n`$uuids` | `['id']` | Defines the fields that will be treated as UUID. By default we assume it will be a primary key, but it can be any field or fields you want.\n\nNow let's see a two examples which will match those for models that were previously shown.\n\n```php\n\u003c?php\n\nnamespace App\\Entities;\n\nuse Michalsn\\Uuid\\UuidEntity;\n\nclass Project1Entity extends UuidEntity\n{\n    protected $attributes = [\n        'id' =\u003e null,\n        'name' =\u003e null,\n        'description' =\u003e null,\n        'created_at' =\u003e null,\n        'updated_at' =\u003e null,\n        'deleted_at' =\u003e null,\n    ];\n}\n```\n\n```php\n\u003c?php\n\nnamespace App\\Entities;\n\nuse Michalsn\\Uuid\\UuidEntity;\n\nclass Project2Entity extends UuidEntity\n{\n    protected $uuids = ['category_id'];\n\n    protected $attributes = [\n        'id' =\u003e null,\n        'category_id' =\u003e null,\n        'name' =\u003e null,\n        'description' =\u003e null,\n        'created_at' =\u003e null,\n        'updated_at' =\u003e null,\n        'deleted_at' =\u003e null,\n    ];\n}\n```\n\nAnd that pretty much it. No more changes are needed.\n\n### UuidCast\n\n**NOTE: Don't use this casting class if you intend to use the `UuidModel` class.**\n\nSince CodeIgniter now allows the use of custom cast classes in Entities, we've also added our own casting class. This casting class enables the UUID to be converted from byte to string during data retrieval and reverse on data setting. Normally you will not use the cast function as the transition from strings to bytes is done in the `UuidModel` class.  With that in mind, you should only use this cast feature when you want to work with the Entity without using `UuidModel` class later.\n\nThis is a simple example of how we would use our casting class with Entity.\n\n```php\n\u003c?php\n\nnamespace App\\Entities;\n\nclass MyEntity extends Entity\n{\n    protected $casts = [\n        'id' =\u003e 'uuid',\n    ];\n\n    protected $castHandlers = [\n        'uuid' =\u003e 'Michalsn\\Uuid\\UuidCast',\n    ];\n}\n```\n\n## Limitations\n\nFor now this class doesn't support SQLite3 database when you want to strore UUIDs in a byte format.\n\n## Supported UUID versions\n\n* Version 1: Time-based - `uuid1`\n* Version 2: DCE Security - `uuid2`\n* Version 3: Name-based (MD5) - `uuid3`\n* Version 4: Random - `uuid4`\n* Version 5: Name-based (SHA-1) - `uuid5`\n* Version 6: Ordered-Time - `uuid6`\n* Version 7: Ordered-Time Random - `uuid7`\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE) for more information.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichalsn%2Fcodeigniter4-uuid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmichalsn%2Fcodeigniter4-uuid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichalsn%2Fcodeigniter4-uuid/lists"}