I would add an extra field that denotes the implication direction, e.g. Assuming 'A "enables" B' translates to "if A then B" in propositional logic, with the material implication going from the antecendent to the consequent (as it should be):
Add an extra field to your schema, something along the lines of 'direction' (implication direction), that indicates the proper commutation of the expression (say 'left' and 'right'... assuming you only have boolean logic); I'm guessing that if A enables B, then it is not necessarily true that B enables A (in your logical system). You can then specify in the 'conditions' paramter of your HABTM model which direction to filter on, or directly to the Model::find() call. Watch out though. Propositional logic can be quite fickle if you don't adhere to the axioms of boolean logic, causing inconsistencies that can make your head hurt. If you're allowing your users to create their own relationships between entities, I would strongly suggest picking up a book/article/mathematician-friend (hi!) to evaluate the nature of your relationships and logical connectives. However, this might be overkill... it all depends on what you are attempting to accomplish. Hope that was somewhat useful. -Joel. On Jun 30, 6:10 pm, Bart van Wissen <[EMAIL PROTECTED]> wrote: > I'm building an application which deals with "alternatives". I won't > go into the details of the application, but what's important here is > that these alternatives have relationships between them. These include > "enables", "excludes", "forbids", etc. For example, "alternative A > forbids alternative B" means that after choosing alternative A, > alternative B cannot be chosen. > > Now I was wondering what would be the best way to model this in > CakePHP (and MySQL). For a given alternative, I want to get all > related alternatives. The way I do it now is by using a table called > "alternative_relationships", which has two foreign keys, > "alternative_1" and "alternative_2", and a relationship name. > In my Alternative model, I put this: > > var $hasAndBelongsToMany = Array( > 'RelatedAlternative' => Array( > 'className' => 'Alternative', > 'joinTable' => 'alternative_relationships', > 'foreignKey' => 'alternative_1', > 'associationForeignKey' => 'alternative_2' > ) > ); > > This way, I only get the relationships that have this alternative at > the 'left' side of the relation. > So for example, if there is a relationship "A forbids B", then for > alternative A I would get alternative B as a related alternative, but > for alternative B, I won't get A. > > So I added the inverse version to get those too: > > var $hasAndBelongsToMany = Array( > 'RelatedAlternative' => Array( > 'className' => 'Alternative', > 'joinTable' => 'alternative_relationships', > 'foreignKey' => 'alternative_1', > 'associationForeignKey' => 'alternative_2' > ), > 'InverseRelatedAlternative' => Array( > 'className' => 'Alternative', > 'joinTable' => 'alternative_relationships', > 'foreignKey' => 'alternative_2', > 'associationForeignKey' => 'alternative_1' > ) > ); > > This way, I get all relationships which have this alternative on > either side of the relation (so either alternative_1 or > alternative_2). I can't help but feel that this is not a very pretty > solution though, because now they end up in separate arrays. > > It would be much nicer if I could get all related alternatives > together, and distinguish the 'direction' of the relationships by > using separate names for the inverse relations (like "forbids" and "is > forbidden by"). > An easy solution would be to insert additional records in the > relationships table for the inverse relationships, but I don't like > having redundant data in my database, because it's hard to manage and > easy to mess up. > > I would love to read your ideas about this. How would you store this > kind of information, and how would you model it in CakePHP? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---
