james cowie Fri Jul 15 07:16:37 -0400 2011

Subject: Help with relationships

Hello I have to tables Hardware & HardwareHosts. hardware has ID and name and HardwareHosts has id, hardwareId and IP.

I have 2 classes Hardware & HardwareHosts

class Hardware extends ActiveRecord\Model {
// user the core database here.
static $connection = 'core';

//set the table name
static $table_name = 'hardware';
//set up the associations with hardware hosts.
static $has_many = array(array('HardwareHosts' , 'foreign_key' => 'hardwareId'));

}

class HardwareHost extends ActiveRecord\Model {
// user the core database here.
static $connection = 'core';

//set the table name
static $table_name = 'hardwareHosts';
static $belongs_to = 'Hardware';
}

If i then do var_dump(Hardware::all()); I dont get any relationships. Just empty....

What am I doing wrong its driving me insane. Any help would be great.


james cowie Fri Jul 15 07:27:12 -0400 2011

ok Ive worked it out:

$hardware = Hardware::find_by_id('847' , array('include' => array('hardwarehosts')));

Now how can I access the array __relationships in my code?

Many Thanks

James

Nanne Huiges Mon Jul 18 06:14:48 -0400 2011

You don't need to include it, it's just not picked up yet. It gets filled on a need-to-know base :)

Don't get hung up on the var_dumps, you're not accessing everything like that. If you want to access the relationships, don't do it trough _relationships. If it starts with _ , don't touch it :D

If all is configured correctly, just call $hardware->hardwarehosts to get your hardwarehosts object.

(1-2/2)