ActiveRecord update primary key

Hi all

i want to ask, i have table like this (dump from postgresql):

CREATE TABLE country (

    country_code character varying(3) NOT NULL,

    country_name character varying(30) NOT NULL



    ADD CONSTRAINT country_country_name_key UNIQUE (country_name);


    ADD CONSTRAINT country_pkey PRIMARY KEY (country_code);

i use CRUD to manage this table,  first i having a problem at actionCreate:


I don't know why '$country->attributes' only contain country_name use above command

so i use this way:



My first question how is the proper way to insert table with primary key.

Second i try to update this table, using the same method of insert, i got error:

                        The requested country does not exist.

I guess AR generate query like this:

                        UPDATE country SET country_code='new-code',country_name='country-name' WHERE country_code='new-code';

I think the condition should be country_code='old-code'

My question is how i set criteria when do update (using save() ??), or what is the right way to update? I don't want to change my table schema (add a autoincrement primary key)

thanks in advance


updated you table? Check model class, maybe is wrong now. Or generate new model class.

still problem with the update

generated _form, didnot generate country_code as i expected so i edit _form and add CHtml::activeTextField for the country_code

nothing happen when i click Update button

How i could use AR for Update the primary key of my country table (i could change the schema but its seems doesnt right, 'cause there are some table which primary key is not serial/autoincrement) ?

Maybe try tun CRUD on model again? I remeber that I had similar problem. The fault was case sensitive name model in controller. Check this.

How i could use AR for Update the primary key of my country table


$model->country_code = value;

You need to use CActiveRecord::updateByPk().

Country::model()->updateByPk($oldCountryCode, $attributes);

Note, validation is not performed by this method.

hm already look Yii Class Reference before but didn't see that, thanks a lot qiang. i have been looking for this

about the validate, i could use validate from model, right? it's written in API (tommorow i will try this)

thanks again guys