Stephen Patterson Sat Jan 26 07:48:05 -0500 2013

Subject: $belongs_to relationship from existing SQL structure, custom column & class names

New to active record, I've an existing DB structure & am using custom class names to avoid collision with existing classes. Already spent a few hours through the docs, forum & examples to no avail, so if anyone can help that would be most appreciated.

Summary db structure:
Table: 'user', class UserRecord, has primary key 'id'
Table: 'topic', class TopicRecord has foreign key 'user' references user(id)

What's the correct $beongs_to for the UserRecord class & what should $has_many in TopicRecord read?


shafiq.rst khan Tue Jan 29 02:08:16 -0500 2013

name the tables as users and topics
user foreign key as user_id
In topic model
static $belongs_to = array(
array('users', 'class_name' => 'UserRecord'),
);
In User Model
static $has_many = array(
array('topics', 'class_name' => 'TopicRecord'),
);

Stephen Patterson Tue Jan 29 10:12:56 -0500 2013

Unfortunately I need to keep the current database structure, we're applying ActiveRecord to an existng data set.

Antoine Sledge Thu Feb 07 10:21:47 -0500 2013

If you haven't already figured it out, within array('topics','class_name'=>'...','foreign_key=>'user').

There's a foreign_key parameter that I'd personally use every time.

You should post your questions to stackoverflow if it takes a while to get an answer here. There are some people a little quicker to answer over there since these forums have quieted down.

(1-3/3)