Modules

ORM_Behavior_Guid
extends ORM_Behavior
extends Kohana_ORM_Behavior

Class declared in MODPATH/orm/classes/ORM/Behavior/Guid.php on line 3.

Properties

protected Database_Query_Builder_Select $_config

Database query builder

Default value:
NULL

protected string $_guid_column

Table column for GUID value

Default value:
string(4) "guid"

protected boolean $_guid_only

Allow model creaton on guid key only

Default value:
bool TRUE

Methods

public on_construct( ORM $model , mixed $id ) (defined in ORM_Behavior_Guid)

Constructs a new model and loads a record if given

Parameters

  • ORM $model required - The model
  • mixed $id required - Parameter for find or object to load

Source Code

public function on_construct($model, $id)
{
	if (($id !== NULL) AND ! is_array($id) AND ! ctype_digit($id))
   {
     if (UUID::valid($id))
     {
       $model->where($this->_guid_column, '=', $id)->find();
       
       // Prevent further record loading
       return FALSE;
     }
   }
   
   return TRUE;
}

public on_create( ORM $model ) (defined in ORM_Behavior_Guid)

A new model is created, add a guid value

Parameters

  • ORM $model required - The model

Source Code

public function on_create($model)
{
  $this->create_guid($model);
}

public on_update( ORM $model ) (defined in ORM_Behavior_Guid)

The model is updated, add a guid value if empty

Parameters

  • ORM $model required - The model

Source Code

public function on_update($model)
{
  $this->create_guid($model);
}

public static factory( string $behavior [, mixed $config = NULL ] ) (defined in Kohana_ORM_Behavior)

Creates and returns a new ORM behavior.

Parameters

  • string $behavior required - $type Type name
  • mixed $config = NULL - $id Parameter for find()

Tags

  • Chainable -

Return Values

  • ORM

Source Code

public static function factory($behavior, $config = NULL)
{
	if ( ! is_string($behavior) AND is_array($config))
	{
		if ( ! is_callable($config))
			throw new Kohana_Exception('Behavior cannot be created: function does not exists');

		// This is either a callback as an array or a lambda
		return new ORM_Behavior_LocalBehavior($config);
	}

	// Set class name
	$behavior_name = 'ORM_Behavior_'.ucfirst($behavior);

	return new $behavior_name($config);
}

protected __construct( array $config ) (defined in ORM_Behavior_Guid)

Constructs a behavior object

Parameters

  • array $config required - Configuration parameters

Source Code

protected function __construct($config)
{
  parent::__construct($config);
  
  $this->_guid_column = Arr::get($config, 'column', $this->_guid_column);
  $this->_guid_only = Arr::get($config, 'guid_only', $this->_guid_only);
}

private create_guid( ) (defined in ORM_Behavior_Guid)

Source Code

private function create_guid($model)
{
  $current_guid = $model->get($this->_guid_column);

  // Try to create a new GUID
  $query = DB::select()->from($model->table_name())
    ->where($this->_guid_column, '=', ':guid')
    ->limit(1);

  while (empty($current_guid))
  {
    $current_guid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
      // 32 bits for "time_low"
      mt_rand(0, 0xffff), mt_rand(0, 0xffff),

      // 16 bits for "time_mid"
      mt_rand(0, 0xffff),

      // 16 bits for "time_hi_and_version",
      // four most significant bits holds version number 4
      mt_rand(0, 0x0fff) | 0x4000,

      // 16 bits, 8 bits for "clk_seq_hi_res",
      // 8 bits for "clk_seq_low",
      // two most significant bits holds zero and one for variant DCE1.1
      mt_rand(0, 0x3fff) | 0x8000,

      // 48 bits for "node"
      mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
    );

    $query->param(':guid', $current_guid);
    if ($query->execute()->get($model->primary_key(), FALSE) !== FALSE)
    {
      Log::instance()->add(Log::NOTICE, 'Duplicate GUID created for '.$model->table_name());
      $current_guid = '';
    }
  }

  $model->set($this->_guid_column, $current_guid);
}