Steven Jones Sun Feb 19 16:02:45 -0500 2012

Subject: Active record does not see my ID column

Hello i'm using active record in Code Igniter through sparks and i have a problem with accessing the ID of a field from the following schema:
table name - users
ID bigint(20) NOT NULL PRI auto_increment
user_name varchar(40) NOT NULL MUL
user_pass varchar(128) NOT NULL
user_email varchar(100) NOT NULL UNI
user_type tinyint(1) NOT NULL
user_url varchar(100) NULL
user_register datetime NOT NULL
user_nicename varchar(40) NOT NULL
display_name varchar(250) NOT NULL

The problem is that whenever i do something like:
$user = User::find_by_username('somename');
if($user)
echo $user->ID;

I get an exception with the message: "Undefined property: User->ID ...
Stack trace: #0 /var/www/platform/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Model.php(333): ActiveRecord\Model->read_attribute('ID') #1 /var/www/platform/application/models/User.php(50): ActiveRecord\Model->__get('ID') #2 .."

My model class:
class User extends ActiveRecord\Model { # explicit table name
static $table_name = 'users';
static $primary_key = 'ID';
...

I've use the non-standard ID name for my PK, but i declared it in my model(static $primary_key = 'ID';).

I've also looked in the files mentioned by the stack trace and could not find anything to solve this problem.

PHP version: 5.3.10
MySQL version: 5.5.20 MySQL Community Server

Any help would be apreciated. Thanks in advance.


Nanne Huiges Sun Feb 19 16:26:41 -0500 2012

As all the members of a PHPAR model are lowercased, you should access it using

$user->id
Steven Jones Mon Feb 20 02:26:14 -0500 2012

It works, thank you.

Sven Leuschner Thu Mar 07 04:56:11 -0500 2013

how can I change that annoying behaviour ?

I can call my columns with lowercase names - but if I do an update via

$result->save();
PHPAR barfs at me "Fatal error: Uncaught exception 'ActiveRecord\UndefinedPropertyException' with message 'Undefined property: User->PKID in ... 'lib/Model.php' ...

so .. where can I switch it OFF? - I have defined column names for a serious reason in Uppercase and I want to keep mynames - it would be very annoying having to rename columns in the entire database as it contains more than 50 tables with around 300 columns - and most of them are uppercase

PHPAR V 1.0 , PHP 5.3.10ubuntu
code

<?php
require_once 'include/php-activerecord/ActiveRecord.php';

class User extends ActiveRecord\Model {
    static $table_name='Users';
    static $primary_key='PKID';
}

Nanne Huiges Thu Mar 07 05:01:43 -0500 2013

No, you don't understand: mysql is case-insensitive, so it doesn't matter how your columns are called (this is NOT the case for tables by the way).

So keep your columns as they are, but - IN PHP -, use only lowercase and you'll be fine. I agree that it might be annoying, but it is probably not worth it to change in the library on your own for compatibility reasons.

Just write 'pkid' everywhere in PHP and keep it as is in MySQL, and you're done.

Sven Leuschner Thu Mar 07 06:19:28 -0500 2013

It seems you don't understand either ...

I DID call them lowercased ...

$result=User::find_by_username($postname);
if ($result===NULL) {  // unknown User
    return array(FALSE
}
//... continues with password-check
// OK all done, now update activitydate for the user
$result->lastactivitydate=new DateTime();
$result->lastlogindate=new DateTime();
$result->customerid=session_id();
$result->save(); // produces said error

the code works - but when I want to update the user with lastactivitydate and lastlogindate the save()-method returns the error
Fatal error: Uncaught exception 'ActiveRecord\UndefinedPropertyException' with message 'Undefined property: User->PKID in /www/dev.bla/include/php-activerecord/lib/Model.php on line 521' in /www/dev.bla/include/php-activerecord/lib/Model.php:521 Stack trace: #0 /www/dev.bla/include/php-activerecord/lib/Model.php(333): ActiveRecord\Model->read_attribute('PKID') #1 /www/dev.bla/include/php-activerecord/lib/Model.php(1030): ActiveRecord\Model->__get('PKID') #2 /www/dev.bla/include/php-activerecord/lib/Model.php(1016): ActiveRecord\Model->values_for(Array) #3 /www/dev.bla/include/php-activerecord/lib/Model.php(855): ActiveRecord\Model->values_for_pk() #4 /www/dev.bla/include/php-activerecord/lib/Model.php(777): ActiveRecord\Model->update(true) #5 /www/dev.bla/include/class_mainapp.php(1219): ActiveRecord in /www/dev.bla/include/php-activerecord/lib/Model.php on line 521 
Nanne Huiges Thu Mar 07 06:29:29 -0500 2013

I did understand :)

You must call your columns in lowercase everywhere in php, so also in the definitions of your class. You probably still have:

static $primary_key='PKID';

in your definition? Because that's not lowercase :D

Sven Leuschner Thu Mar 07 07:04:14 -0500 2013

well of course I still had - "your" desciption said to set the static $primary-key if my pk-column was not called `id` - so I changed it to what it's really called ;-)
that works now .. thanks for clarifying

Juan Medina Fri Mar 04 08:57:23 -0500 2016

I have a similar problem, but I can't solved it. My fields id are in database as ID and I can't access to it. I change the static $primary_key for id and ID in model but nothing. I can use find() and work, but if I use user->id, it doesn't work. Any suggestion?

(1-8/8)