Open IllyaMoskvin opened 8 years ago
This seems a bit insane to me, but I imagine that a nested has_many through relationship might be defined as follows:
class Category extends ActiveRecord\Model {
static $table_name = 'entity_categories';
static $primary_key = 'id';
static $has_many = array(
// This works currently
array( 'entries', 'through' => 'category_entry_links' ),
// This should(?) work
array( 'entry_object_links', 'through' => array('entries', 'through' => 'category_entry_links') ),
array( 'objects', 'through' => array('entry_object_links', 'through' => array('entries', 'through' => 'category_entry_links') ) ),
);
}
Alternatively, I tried defining static $delegate = array( array('objects', 'to' => 'entries') );
for kicks, since $entry->objects
would be a valid call, but no luck: I got an Call to member function __get()
error.
I'm afraid such a relation is currently not possible. What I usually do in cases like this, is adding a get_association
method to the model. In your case get_objects
and write the join myself;
Object::find('all', [
'join' => 'JOINS ... ON ...',
'conditions' => ['category_id' => $this->id]
]);
This is just of the top of my head so it could be that I made a mistake
This is not so much a feature request, but rather, more of a question about best practices with the current version of PHP AR. Let's say that I have three entity types, which are connected in sequence through
xref
relationship tables:There are two
has_many through
relationships here. In plain-speak, a category has multiple entries, an entry can belong to multiple categories, an entry features multiple objects, and an object can be featured in multiple entries.I've set up my models. Everything works great. Leaving out the lengthy fields, I can do, for instance:
...and get the following:
However, what if I'd like the option to receive this JSON instead? Note that duplicate objects are automatically removed:
Is there any easy way to set up my models to make serialization painless? For instance:
Likewise,
$category->entries
returns an array of entries associated with that category. How should I configure my models so that$category->objects
would return an array of objects associated with the entries that are associated with that category, with duplicates removed?Apologies for the long, round-about post. I couldn't think of a better way to explain my situation without giving real-world examples. I'm also not sure if this question has been asked before; I searched, but had no luck. If there are previous discussions on this topic, please do link me to them. If you'd like me to share my models, please ask, but I think the setup is pretty intuitive given the tables involved. I'll keep working on this problem in the meantime. Thank you for your time.