Eric Siegel Fri Dec 14 10:02:02 -0500 2012
I posted this on StackOverflow (http://stackoverflow.com/q/13868791/206403), and I got an answer there.
The solution was to be as explicit as possible (and to note that php.activerecord FORCES all field names to be lower case).
In ContactCompanyLink.php, I added:
static $belongs_to = array(
array(
'contact', # The table name it's linked to
'foreign_key' => 'inspectorid', # key in linked (this) table
'primary_key' => 'contactid', # key in "parent" table
)
);
and then in Contact.php, I have:
static $has_many = array(
array(
'companies', # the property name in the Contact object
'foreign_key' => 'inspectorid', # key in linked table
'primary_key' => 'contactid', # key in "parent" table
'class_name' => 'ContactCompanyLink'
)
);
Now "var_dump(Contact::find(1234)->companies)" works!
(1-1/1)
Subject: Model association with custom table and key names
I am trying to link two tables.
I have "contacts" and "contactCompanyLinks". Each contact can contain one or more rows in the link table. The field names in the table don't follow any sensible convention, but I can't change it as it would break another app using the same table.
The primary key of "contacts" is called "contactID", and the foreign key in "contactCompanyLinks" is called "inspectorID".
php.activerecord makes way too many assumptions, and I can't figure out how to get it to link my tables together.
Here are my models:
Contact.php:
ContactCompanyLink.php:
This looks right, but when I tried to get a row, it's not working. I did the following:
All I got printed to the screen was "NULL"! If I tried other properties like "contactcompanylink" or "ContactCompanyLink" or "ContactCompanyLinks", I got an "undefined property" error.
works just fine. It shows me the fields from the "contacts" table.
How do I tell php.activerecord to stop assuming things and listen to me when I try to tell it how to link 2 tables together?