RedBeanPHP
The Power ORM

UUIDs

RedBeanPHP has not been designed for use with UUIDs or GUIDs. However if you really want, you can tune RedBeanPHP to support this.

Enabling UUID support in MySQL

To enable UUID support in MySQL in fluid and frozen mode you need to provide your own QueryWriter. Here is an example of a QueryWriter that enables UUIDs in MySQL:

    class UUIDWriterMySQL extends MySQL {

        protected 
$defaultValue '@uuid';
        const 
C_DATATYPE_SPECIAL_UUID  97;

        public function 
__constructAdapter $adapter ) {
            
parent::__construct$adapter );
            
$this->addDataType(
            
self::C_DATATYPE_SPECIAL_UUID'char(36)'  );
        }

        public function 
createTable$table ) {
            
$table $this->esc$table );
            
$sql   "
                CREATE TABLE 
{$table} (
                id char(36) NOT NULL,
                PRIMARY KEY ( id ))
                ENGINE = InnoDB DEFAULT
                CHARSET=utf8mb4
                COLLATE=utf8mb4_unicode_ci "
;
            
$this->adapter->exec$sql );
        }

        public function 
updateRecord$table$updateValues$id NULL ) {
            
$flagNeedsReturnID = (!$id);
            if (
$flagNeedsReturnIDR::exec('SET @uuid = uuid() ');
            
$id parent::updateRecord$table$updateValues$id );
            if (
$flagNeedsReturnID $id R::getCell('SELECT @uuid');
            return 
$id;
        }

        public function 
getTypeForID(){
            return 
self::C_DATATYPE_SPECIAL_UUID;
        }
    }

Now you need to rewire the objects to swap the old Query Writer for the new one:

        $oldToolBox R::getToolBox();
        
$oldAdapter $oldToolBox->getDatabaseAdapter();
        
$uuidWriter = new UUIDWriterMySQL$oldAdapter );
        
$newRedBean = new OODB$uuidWriter );
        
$newToolBox = new ToolBox$newRedBean$oldAdapter$uuidWriter );
        
R::configureFacadeWithToolbox$newToolBox );

Depending on the namespaces and aliases you use you might have to adjust this code accordingly.

Enabling UUID support in PostgreSQL

To install the UUID module for PostgreSQL run the following query:

    CREATE EXTENSION "uuid-ossp";

This command requires at least PostgreSQL version 9.1, for earlier versions of Postgres please consult their documentation.

Here is an example class for PostgreSQL:

    class UUIDWriterPostgres extends PostgreSQL {

        protected 
$defaultValue 'uuid_generate_v4()';
        const 
C_DATATYPE_SPECIAL_UUID  97;

        public function 
__constructAdapter $adapter ){
            
parent::__construct$adapter );
            
$this->addDataTypeself::C_DATATYPE_SPECIAL_UUID'uuid'  );
        }

        public function 
createTable$table ){
            
$table $this->esc$table );
            
$this->adapter->exec"
            CREATE TABLE 
$table (id uuid PRIMARY KEY); " );
        }

        public function 
getTypeForID(){
            return 
self::C_DATATYPE_SPECIAL_UUID;
        }
    }

These are just examples, they allow you to use UUID but they may not fit your needs. I recommend to craft your own Query Writer, tailored to your needs.


 

RedBeanPHP Easy ORM for PHP © 2017 () and the RedBeanPHP community () - Licensed New BSD/GPLv2 - RedBeanPHP Archives