PHP code example of michalsn / codeigniter4-uuid
1. Go to this page and download the library: Download michalsn/codeigniter4-uuid library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
michalsn / codeigniter4-uuid example snippets
$psr4 = [
'Config' => APPPATH . 'Config',
APP_NAMESPACE => APPPATH,
'App' => APPPATH,
'Michalsn\Uuid' => APPPATH . 'ThirdParty/codeigniter4-uuid/src',
];
$uuid = service('uuid');
// will prepare UUID4 object
$uuid4 = $uuid->uuid4();
// will assign UUID4 as string
$string = $uuid4->toString();
// will assign UUID4 as byte string
$byte_string = $uuid4->getBytes();
namespace App\Models;
use Michalsn\Uuid\UuidModel;
class Project1Model extends UuidModel
{
protected $table = 'projects_1';
protected $primaryKey = 'id';
protected $returnType = 'array';
protected $useSoftDeletes = true;
protected $allowedFields = ['name', 'description', 'created_at', 'updated_at', 'deleted_at'];
protected $useTimestamps = true;
protected $validationRules = [
'name' => '
namespace App\Models;
use Michalsn\Uuid\UuidModel;
class Project2Model extends UuidModel
{
protected $uuidFields = ['category_id'];
protected $table = 'projects_2';
protected $primaryKey = 'id';
protected $returnType = 'array';
protected $useSoftDeletes = true;
protected $allowedFields = ['category_id', 'name', 'description', 'created_at', 'updated_at', 'deleted_at'];
protected $useTimestamps = true;
protected $validationRules = [
'category_id' => '
namespace App\Entities;
use Michalsn\Uuid\UuidEntity;
class Project1Entity extends UuidEntity
{
protected $attributes = [
'id' => null,
'name' => null,
'description' => null,
'created_at' => null,
'updated_at' => null,
'deleted_at' => null,
];
}
namespace App\Entities;
use Michalsn\Uuid\UuidEntity;
class Project2Entity extends UuidEntity
{
protected $uuids = ['category_id'];
protected $attributes = [
'id' => null,
'category_id' => null,
'name' => null,
'description' => null,
'created_at' => null,
'updated_at' => null,
'deleted_at' => null,
];
}
namespace App\Entities;
class MyEntity extends Entity
{
protected $casts = [
'id' => 'uuid',
];
protected $castHandlers = [
'uuid' => 'Michalsn\Uuid\UuidCast',
];
}