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')
);
}
Christopher Coleman Thu Dec 22 12:02:32 -0500 2011
I am also having this problem with eager loading.
The issue is specifically the eager loading, K. LR. I have my models and relationships set up just as you suggest, but it doesn't work.
Or rather, if you just try to access the property it works fine, but if you try to eager load the property via an 'include' during a Model::find(...), it fails.
Christopher Coleman Thu Dec 22 13:27:37 -0500 2011
This appears to be the culprit:
Relationship.php [151-173] with my fix on line 168
1 if (!empty($options['through'])) { 2 // save old keys as we will be reseting them below for inner join convenience 3 $pk = $this->primary_key; 4 $fk = $this->foreign_key; 5 6 $this->set_keys($this->get_table()->class->getName(), true); 7 8 if (!isset($options['class_name'])) { 9 $class = classify($options['through'], true); 10 $through_table = $class::table(); 11 } else { 12 $class = $options['class_name']; 13 $relation = $class::table()->get_relationship($options['through']); 14 $through_table = $relation->get_table(); 15 } 16 $options['joins'] = $this->construct_inner_join_sql($through_table, true); 17 18 $query_key = $fk[0]; <-- FIX: was $this->primary_key[0] 19 20 // reset keys 21 $this->primary_key = $pk; 22 $this->foreign_key = $fk; 23 }
With this fix, my many to many models eagerly load during a find as expected and I haven't (yet) noticed anything breaking.
(1-3/3)
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?