UUID/Dev Defined Primary Key

How would I use a UUID as a primary key when a new record is posted under the create action,

I want to generate a UUID (32 character value) or any other calculated PK value and set it to the PK column before being saved.

Would be cool if the PK is defined as a CHAR(32), automatically populates with a UUID value.

Thanks,

Edward

There are many UUID generation scripts around…

I did that by extending the CActiveRecord class. My tables all have an id field named 'id' of type char(30). My models extend the UniqidActiveRecord class.

This is a very simple example. I planned to lookup the primary key attribute so it will support any primary key name, but it worked so well that I have spent my time on other things. And I like calling my primary keys 'id'.



class UniqidActiveRecord extends CActiveRecord {





  protected function beforeSave() {


    if($this->isNewRecord) {


      $this->id = uniqid('', true);


    }


	 return true;


	}





}


What is the advantage of using a UUID as primary key instead of an Incremental field?

In most cases: none.

I have a copy of my database and application set up on a laptop with two-way replication, so I can update the data on the laptop while being offline. Generating "unique" keys prevents primary key conflicts when replicating the databases.

I could have used auto_increment_increment  auto_increment_offset in mysql config, but that would have affected the databases not being replicated.

I am new to Yii. I like the framework very much so far. I am looking at the rsubonic and I see that an extension was created to handle the uuid. I am wondering, where do I place this file? I am not sure where to place this file in order to get Yii to recognize its existence. I have to do something similar to this to keep track of who created the record and who is updating the record.

Thanks

Which extension are you referring to? If you get it from http://www.yiiframew…om/extensions/, you should follow its installation instructions. If it is a class file, place it anywhere under ‘protected’, and then import it with path alias ‘application.path.to.ClassName’ before you use it.

Sorry to not be so descriptive. I am just trying to figure out where to place the UniqidActiveRecord class script file. I tried placing it in components but Yii couldn't figure out where it was located. Thanks

import it before you use it:

Yii::import('application.components.UniqidActiveRecord');

If you are importing "application.components.*", then you can use any class under the "components" directory without any further import statement.

I was importing the application.component.* directory but for some reason it wouldn't work. I let my models extend the UniqidActiveRecord class. I will try again. Thanks again.

I am not sure what I am doing wrong. I created the file called UniqdActiveRecord.php and placed it in application.components. I changed the models, that I wanted to use this class, to extend the UniqdActiveRecord. Whenever I try to access one of the controllers I get the following error message. Not exactly sure what I am doing wrong. Thanks

YiiBase::include_once(UniqidActiveRecord.php) [<a href='yiibase.include-once'>yiibase.include-once</a>]: failed to open stream: No such file or directory

Check your spelling? I saw you are using UniqidActiveRecord and UniqdActiveRecord.

This is the path to my class file:  protected/lib/UniqidActiveRecord.php

I added the lib directory to the import setting in config/main.php (application.lib.*):

 


  'import'=>array(


    'application.models.*',


    'application.components.*',


    'application.lib.*',


  ),


My models are the declared like this:



class Event extends UniqidActiveRecord


{


......


Hope this helps.

I had a typo. Sorry about the mistake.