Jake Hawkes Sun Jun 27 01:31:45 -0400 2010

Subject: Reference Table

Is there any way to set up an association for a reference table? Let's say I'm creating a warehouse app and I have a class called Item which has a property called status (which is a Status object). I only have a handful of different Statuses, and I don't want to create a new entry in the database for every single Item's Status. How could I do this?

I apologize if this is covered somewhere on the wiki. I've scoured the entire thing several times and can't seem to find it mentioned.


Kien La Sun Jun 27 19:53:54 -0400 2010

Sounds like you just want to use a belongs_to relationship:

1 class Status extends ActiveRecord\Model {
2 }
3 
4 class Item extends ActiveRecord\Model {
5   static $belongs_to = array(array('status'));
6 }
7 
8 Item::find(123)->status->name;

I don't know what your tables look like so this code would assume the standard conventions php-activerecord uses which would be:

 1 CREATE TABLE statuses(
 2   id not not null primary key auto_increment,
 3   name varchar(25)
 4 );
 5 
 6 CREATE TABLE items(
 7   id int not null primary key auto_increment,
 8   status_id int
 9 );

(1-1/1)