Craig Marshall Sun Aug 08 07:47:41 -0400 2010

Subject: belongs_to relationship

Hi, I'm having trouble getting basic relationships to work. I have a yarn table and a yarn_blends table. They both have the standard id primary key. The yarn table also has yarn_blend_id.

I've created models called yarn and yarn_blends, and in the yarn model declared that the table 'belongs_to' the yarn_blends table.

I'm getting a 'Trying to get property of non-object' error. Is this because I don't have foreign keys set up? I'm assuming that phpactiverecord would be able to open the related tables/objects with just the information given in the model definition.

If I need to set up foreign keys, can I do it on MyISAM tables, or do I need to change them to InnoDB?

Thanks


Jacques Fuentes Sun Aug 08 12:39:58 -0400 2010

Could you please provide the code for your models so and an example of what you're doing? This will make it easier to diagnose.

Craig Marshall Sun Aug 08 14:11:56 -0400 2010

Hi Jacques,

Thanks for the quick response. I've now deleted the original code that I was trying to make work and replaced it with getters and setters (Not tried saving yet, with this approach though). I will put the code back to how it was before...

Typically - after rewriting the code, it works. Thanks for the encouragement to look closer at it, and sorry for wasting your time.

I must have had a typo. Here is my code now, which works perfectly:

class yarn extends ActiveRecord\Model {
static $belongs_to = array(array('blend', 'class_name' => 'yarn_blend', 'foreign_key' => 'yarn_blend_id'));
}

class yarn_blend extends ActiveRecord\Model {}

Thanks again,
Craig

(1-2/2)