Yii Framework Forum: How does ActiveRecord retrieve the primary key of an SQL table? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How does ActiveRecord retrieve the primary key of an SQL table? Rate Topic: -----

#1 User is offline   thyseus 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 300
  • Joined: 18-April 09
  • Location:Leipzig, Germany

Posted 04 November 2009 - 06:21 AM

Hi.

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)

$criteria=$this->getCommandBuilder()->createPkCriteria($this->getTableSchema(),$pk,$condition,$params)


to get table metadata information.

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

$this->getMetaData() looks like this:

	public function getMetaData()
	{
		if($this->_md!==null)
			return $this->_md;
		else
			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.
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,879
  • Joined: 04-October 08
  • Location:DC, USA

Posted 04 November 2009 - 07:12 AM

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

#3 User is offline   thyseus 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 300
  • Joined: 18-April 09
  • Location:Leipzig, Germany

Posted 04 November 2009 - 07:45 AM

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() {
                $dsn="pgsql:host=<>;port=5432;dbname=<>";
                $user="<<";
                $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"?
0

#4 User is offline   thyseus 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 300
  • Joined: 18-April 09
  • Location:Leipzig, Germany

Posted 04 November 2009 - 07:46 AM

Empty (sorry for double-post)
0

#5 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 04 November 2009 - 08:07 AM

View Postthyseus, on 04 November 2009 - 07:45 AM, said:

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


http://php.net/manua...p5.patterns.php
0

#6 User is offline   thyseus 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 300
  • Joined: 18-April 09
  • Location:Leipzig, Germany

Posted 04 November 2009 - 08:12 AM

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?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users