How does ActiveRecord retrieve the primary key of an SQL table?


I try to use the yii framework without the CWebApplication class.

So i decided to make my model from scratch.

The Yii-Documentation says, this is the raw frame for an AR-Model:

class Model extends CActiveRecord {

  public static function model($className=__CLASS__)


        return parent::model($className);



Assuming the DB Connection works, the table is called Model, and entry #766 exists, the following should work:

$m = new Model;

try { $m->findByPk(766); }

catch (Exception $e ) { echo "<pre>"; echo $e->getMessage(); print_r ($e->getTrace()); 

It does not. I get the following Error message:

Column name must be either a string or an array.

Now, my Investigations did come so far:

The classCActiveRecord uses (line 1184)


to get table metadata information.

getTableSchema() uses $this->getMetaData() to gather information.

$this->getMetaData() looks like this:

	public function getMetaData()



			return $this->_md;


			return $this->_md=self::model(get_class($this))->_md;

Now: How the hell is the Primary Key of an Active Record gathered from the database??

I use the pgsql connector.

Any ideas? Thanks for following my thoughts so far, and for any explanation.

Addition: even setting public function primary_key() {return ‘pk_field’; } doesn´t work.

In order to use AR without application, you need to override its getDbConnection(). Make sure you only create a singleton of the DB connection.

Yes, of course… i forgot to mention it… i did override the getDbConnection function. But it doesn´t work either.

Executing SQL queries do work. The connection is definitely fine.

This is the working code, with anonymized connection strings:

        function getDbConnection() {



                $pass = "<>";

                $connection = new CDbConnection($dsn, $user, $pass);

                try {

                                $connection->active = true;


                catch (Exception $e) {

                        echo $e->getMessage();


                return $connection;


What do you mean with "a singleton of the Db Connection"?

Empty (sorry for double-post)

Thank you! This is a good documentation for writing high-quality Code.

And by the way, I solved the problem.

Of course, it needs to be

public function primarykey() {return 'pages_id'; } 

i had to remove the _ between primary and key to get it working. But anyway shouldn´t yii be able to get the primary key automatically from the database table schema?

a print_r of the model shows, how a table column looks like:

    [columns] => Array


                            [id] => CPgsqlColumnSchema Object


                                    [name] => id

                                    [rawName] => "id"

                                    [allowNull] => 

                                    [dbType] => integer

                                    [type] => integer

                                    [defaultValue] => 

                                    [size] => 

                                    [precision] => 

                                    [scale] => 

                                    [isPrimaryKey] => 

                                    [isForeignKey] => 

                                    [_e:private] => 

                                    [_m:private] => 


and i see the "isPrimaryKey" here. Couldn´t it be automatically set when it is set in the database?