george gally Tue Jun 14 12:31:25 -0400 2011

Subject: confuzzled with simple associations

i'm just trying to get the basic stuff going, and it's got me stumped. have experience in rails, checked out the examples, but still no luck.

have an artist class:

class Artist extends ActiveRecord\Model {
static $has_one = array(
array('profile')
);
}

and a profile class:

class Profile extends ActiveRecord\Model {
static $belongs_to = array(
array('artist')
);

}

and my tables look like standard association tables
table 'artists' has: id, name, etc.
table 'profiles' has: artist_id, bio, etc.

however, if i do:
$artist = Artist::find_by_name('Maxxtor');
it brings back the entire Artist record
but if i now try to access anything in profile , eg.
echo $artist->profile->bio;

i get a "Notice: Trying to get property of non-object" error

any ideas would be much appreciated.


Daniel Lowrey Tue Jun 14 13:07:12 -0400 2011

George, it looks like your associations are set up correctly but you still need to explicitly tell your finder to include the profile association. Try this:

$name = 'Maxxtor';
$conds = array('name = ?', $name);
$inc = array('profile');
$artist = Artist::find('first', array('conditions'=>$conds, 'include'=>$inc));
if ($artist) { print_r($artist); }

You can read more about including associations here: http://www.phpactiverecord.org/projects/main/wiki/Finders#eager-loading

(1-1/1)