Get Primary Key Field Name of a model

Hi all,

How to get the primary key field name of model? The document said the primaryKey() method ‘The default implementation simply returns null, meaning using the primary key defined in the database.’ if not overridden.

Does the following code work?





$attributeNames = $model->attributeNames();

$pkFieldName = $attributeNames[0];




Thanks.

Try this:




$model->tableSchema->primaryKey;



http://www.yiiframework.com/doc/api/1.1/CDbTableSchema

1 Like

It’s the correct way to do it. Thanks a lot.