Thanks, Steve - greatly appreciate your help! I've got two tables:
CREATE TABLE `people` (
`person_id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(100) NOT NULL,
PRIMARY KEY `person_id` (`person_id`)
);
CREATE TABLE `relations` (
`source_id` int(10) unsigned NOT NULL,
`link_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`source_id`,`link_id`)
);
The problem is that some people entries will need to relate to other people entries - what you called a sibling relationship. Let's say that I've got three people:
- Code: Select all
person_id name
---
1 Joe
2 Mary
3 Oscar
What I'd like is to have a relation tab in Xataface that will show me Mary, if I'm in Joe's record, or will show me Joe, if I'm in Mary's record. It's a many-to-many relationship that's feeding back on itself.
Usually I'd just create the relationship table:
- Code: Select all
source_id link_id
---
1 2
and use a query like:
SELECT people.* FROM people p INNER JOIN relations r ON (p.person_id = r.source_id OR p.person_id = r.link_id) WHERE r.source_id = '$person_id' OR r.link_id = '$person_id';
However, Xataface doesn't seem to be happy with putting such a query in the __sql__ relationships.ini. That intuitively makes sense to me (though haven't really thought it through) - it's just too ambiguous for adds/removes.
So my workaround was to add hooks to after_action_new_related_record() et al that would just create a reciprocal relationship:
- Code: Select all
source_id link_id
---
1 2
2 1
Now I can just use this for __sql__:
SELECT people.* FROM people p INNER JOIN relations r ON p.person_id = r.source_id WHERE r.link_id = '$person_id';
which Xataface seems to have no problems using. Displays, adds, etc. are all fine. I did need to insert:
- Code: Select all
import('Dataface/Utilities.php');
Dataface_Utilities::fireEvent('before_action_remove_related_record');
at line 63 of remove_related_record.php (right before the "$form->process(array(&$form, 'delete'), true)") and then add a hook to delete the reciprocal relationship.
Like I said, it's not ideal, but it seems to be working okay so far. My question is really:
1) Have I missed something huge about how I *should* be structuring this? and
2) If not, is there a better method for implementing sibling relationships within Xataface?
Thanks again for your help on this. Please feel free to blow me off if this is too complicated. It seems to be working so far, so this is a sanity check & curiosity. Thanks!