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
-~----------~----~----~----~------~----~------~--~---

Reply via email to