Travis Woolworth Thu May 31 00:01:51 -0400 2012

Subject: Associations not firing, not throwing error

Sorry if this a beginner question but I've tried to scour the documentation and forums looking for someone who's had the same problem as I and I can't seem to find anyone. Basically I have three tables that have associations and when I do a query or find on any of them, the association is never loaded into the object, the weirder part is no error is ever thrown so it's like it's just not firing.

Sorry for the naming scheme but it's from a previous developer / project and I'm not allowed to change it which I think is part of my problem. Here are my three models.

My three models:

class InstallerPostalCodeVO extends ActiveRecord\Model {

// => Explicit table name since our table is not "users" 
static $table_name = 'InstallerPostalCode';
// => Explicit PK since our PK is not "id" 
static $primary_key = 'installerpostalcodeid';
static $belongs_to = array(
array('installer', 'class_name' => 'InstallerVO', 'primary_key' => 'installerid', 'foreign_key' => 'installerid'),
array('postalcode', 'class_name' => 'PostalCodeVO', 'primary_key' => 'postalcodeid', 'foreign_key' => 'postalcodeid')
);
}

class InstallerVO extends ActiveRecord\Model {

// => Explicit table name since our table is not "users" 
static $table_name = 'Installer';
// => Explicit PK since our PK is not "id" 
static $primary_key = 'installerid';
static $has_many = array(
array('postalcodes', 'class_name'=>'InstallerPostalCodeVO', 'foreign_key' => 'installerid')
);
}

class PostalCodeVO extends ActiveRecord\Model {

// => Explicit table name since our table is not "users" 
static $table_name = 'PostalCode';
// => Explicit PK since our PK is not "id" 
static $primary_key = 'postalcodeid';
static $has_many = array(
array('postalcodes', 'class_name'=>'InstallerPostalCodeVO', 'foreign_key' => 'postalcodeid')
);
}

Here is the code I'm using to try and pull an installerpostalcode which should return both the Installer and the PostalCode based on their id's assigned.

$result = InstallerPostalCodeVO::find_all_by_installerid($id, array('include' => array('installer', 'postalcode')));
Returns:
[ {
"installerpostalcodeid": 3,
"installerid": 1,
"postalcodeid": 1
}, {
"installerpostalcodeid": 4,
"installerid": 1,
"postalcodeid": 2
}
]

Here is the code I'm using to try an pull all installer and their assigned postal codes associations.

$installers = InstallerVO::find($id, array('include' => array('postalcodes')))->to_array();
Returns: {
"installerid": 1,
"fullname": "Installer 2"
}

I've tried the code without the include, with the include, changing the options every which way in the has_many, belongs_to, etc.. But nothing ever seems to make a difference, it's like the association code is never fired.. Does anyone have any idea what I'm doing wrong?