Eccoci di nuovoi con cakePHP. Mi auguro che qualcuno sia ferrato in materia per aiutarmi, perché le discussioni relative a questo framework latitano...
Il problema di oggi sono le relazioni molti a molti multiple. Ossia, date le entità A e B queste hanno, ad esempio, due relazioni molti a molti R1 e R2. Ora... se non fossimo alle prese con cakePHP le soluzioni potrebbero essere varie: la più naturale forse fare due tabelle, una per R1 e una per R2. Oppure fare una sola, con un campo che specifica a quale relazione si riferisce ogni riga.
Con cakePHP e le sue convenzioni mi sembra che la scelta sia obbligata: tabella unica. Ma come si deve gestire?
Nel cookbook ho trovato un piccolo riferimento alla questione ma non sono certo di aver capito.
Presa una dichiarazione di associazione molti a molti come la seguente
Codice PHP:
class Recipe extends AppModel {
var $name = 'Recipe';
var $hasAndBelongsToMany = array(
'Tag' =>
array('className' => 'Tag',
'joinTable' => 'posts_tags',
'foreignKey' => 'recipe_id',
'associationForeignKey' => 'tag_id',
'conditions' => '',
'order' => '',
'limit' => '',
'uniq' => true,
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
}
Il manuale spiega che
# foreignKey: the name of the foreign key found in the other model.
This is especially handy if you need to define multiple HABTM relationships. The default value for this key is the underscored, singular name of the other model, suffixed with ‘_id’.
# associationForeignKey: the name of the foreign key found in the current model.
This is especially handy if you need to define multiple HABTM relationships. The default value for this key is the underscored, singular name of the current model, suffixed with ‘_id’.
Si riferisce a multiple HABTM relationships... ma sempre tra gli stessi due modelli? E come si devono definire a livello di DB. Devo usare diverse chiavi nella stessa tabella di associazione oppure devo creare una seconda tabella di associazione?
(notare che spiegando l'uso del parametro join table il manuale non dice che questo torna utile per relazioni HABTM multiple, ma solo nel caso che la tabella di join non aderisca alle convenzioni sui nomi)
Se qualcuno mi dà una dritta mi fa un favorone.
Ad esempio, date le entità Galleria e Utente, e le due relazioni molti a molti Utenti osservatori della galleria e Utenti curatori della galleria, qual è la maniera corretta di strutturare il DB e di dichiarare le associazioni in CakePHP?