CActiveRecord strange issue

I am doing




VehicleFeature::model()->findByPk(200);



And getting back :




The value for the column "feature_id" is not supplied when querying the table "cms_vehicle_feature". 



But if i do




VehicleFeature::model()->find('feature_id=200');



It works perfectly.

I have a table named vehicle, which holds data about vehicles, and this is the table named vehicle_feature, which holds various vehicle features

Vehicle model relations:




public function relations()

{

	return array(

        [...]

	'vehicleFeature' => array(self::HAS_MANY, 'VehicleFeature', 'vehicle_id'),

        [...]

	);

}



VehicleFeature model




public function relations()

{

	return array(

		'vehicle' => array(self::BELONGS_TO, 'Vehicle', 'vehicle_id'),

	);

}



Model::model()->findByPk() should work anyway, no matter the relation between tables, but why is it failing ?

PS: the models are generated via GII, so i believe they are correct.

Maybe your have multiple primary keys?

Yes i have PRIMARY(vehicle_id, feature_id);

But, even if i have multiple primary keys, in the model i have




    public function primaryKey()

    {

        return 'feature_id';

    }



Therefore, shouldn’t AR class look into my table after feature_id as primary key ?

Also, if i have multiple primary keys in a table, how should i fetch by primary key , if not in the way i tried above ? because, honestly it doesn’t make any sense to not work.

composite primary key?

I understood what he meant with multiple primary keys(composite) and the answer is yes, please see my above post, it explains .

It doesn’t work with composite primary key, simply use find.

According to the documentation:

It looks like you are forced to set both.

Thanks Zaccaria, seems like this is how it works:




    //MODEL

    public function primaryKey()

    {

        return array('feature_id, vehicle_id');

    }




    //CONTROLLER CALL OF MODEL

    $vehicleFeature=VehicleFeature::model()->findByPk(array('feature_id'=>(int)$fid,'vehicle_id'=>(int)$vid));



Any other way fails to work .

I guess you may not overload primaryKey() method :

This method is meant to be overridden in case when the table is not defined with a primary key (for some legency database). If the table is already defined with a primary key, you do not need to override this method. The default implementation simply returns null, meaning using the primary key defined in the database.(yiiframework.com/doc/api/1.1/CActiveRecord#primaryKey()-detail)

Don’t worry i solved it as i shown in the previous post, thanks for the help:)

Hi twisted1919,

How you solved problem, I faced same error and tried as you typed at previous post. Would you show whole code?. :) I am confused, where do you define $fid and $vid? In VehicleFeature feature model?

Sorry, I am really newbie in MVC programming.

Thanks in advance.

Dondi.

Thanks twisted1919

i too had the same issue, your post helped, but i had accidentally created a field with primary as primary key.

Also wouldn’t it be better to use updateByPk instead of findAllByPk for updating a row?

regards

charles