Multiple PKs - findByPk


regarding the documentation the findByPk method accepts an array if you have more than one primary key specified in your table

$model = TranslationMessage::model()->findByPk(array($id, $language));

But that doesn’t work and the error “array_keys() expects parameter 1 to be array, string given” shows up.

Any advice?

Thanks in advance!


You just gave the column values, not the column names.

The docu says that the array have to be in format [color="#222222"][font="Arial, sans-serif"][size="2"]column name=>column value[/size][/font][/color]

[font="Arial, sans-serif"][size="2"][color="#222222"][b]


$model = TranslationMessage::model()->findByPk(array('id' => $id, 'language' => $language));


You can also define the composite primary key in your TranslationMessage model class by overriding getPrimaryKey().

Example: Guide - Define AR Class


You should not have more than one primary key in your table. You might have a composite primary key - which is a single primary key consisting of more than one field.

This wiki uses an sql read on such a composite key (see Another use case…) if you would be interested.

Hi Gerhard,

the idea of having two primary keys is not my idea :) … read this:

@kokomoko: Thanks, that works :)

I think you misunderstood him :lol: There is no relational database I am aware of that supports tables with more than one primary key. However, primary keys covering more than one field (a.k.a. composite primary keys) are well supported.

Ohhhh… thanks Da:Sourcerer for clarifying that !! :) Then, I indeed misunderstood him :)

Well Nightmove, if you add secondary indexes and make them each unique, then I guess you could "treat" them like many primary keys. The wiki I mentioned does exactly that in the junction table.

I’m actually going to sit with this same problem soon because I need to create a table where some users want to use a numeric key; others want to use an alpha key; and others an auto-increment key. So I will have to include all three and treat them as different primary keys. Wish me luck.

Let us know if you find an answer to your problem. :)

I actually did solve it:

$model = TranslationMessage::model()->findByPk(array('id' => $id, 'language' => $language));

Thanks for the help @ all and good luck to you Gerhard