K. LR
Mon Jul 11 14:34:40 -0400 2011
Try something like this :
class Concern extends ActiveRecord\Model {
static $table = 'concern';
static $has_many = array(
array('products', 'through' => 'concern_product' )
);
}
class Product extends ActiveRecord\Model
{
static $table = 'product';
static $has_many = array(
array('concern_product')
);
}
class ConcernProduct extends ActiveRecord\Model
{
static $table = 'concern_product';
static $belongs_to = array(
array('concern'),
array('product')
);
}
(1-1/1)
Subject: Problem with eager loading and many-to-many
Hi All,
I'm probably doing something wrong here, but I'm having trouble using many-to-many relationships with eager-loading. Here's my table structure:
then I have the following (partial) model code:
I'm trying to access $concern->products, but it return an empty array. It generates the correct SQL
and seems to pull the correct records, but then they are filtered out by this part of Relationships.php around line 191:
Maybe I'm crazy, but doesn't this code look for a direct relationship between the model and related model? Obviously there isn't one in my case since I have a many-to-many relationship through a third table, but it seems to be looking for a key in my product model that relates to my concern model, which doesn't exist.
I'm using the nightly build. Any ideas on what I'm doing wrong?