Question

After struggling with this inconvenience for a couple of weeks now, I've decided to open en now topic here. A topic that can help me, but I'm sure it will help some others with this same problem too!

I wonder how I should name the tables, controllers and models of a hasMany through table (thus with additional fields) and it's coupled tables. I tried to stick on the CakePHP naming conventions as discribed in it's cookbook, but this constantly gave me some "Object not found" errors. For practical reason, I'll show you my problem with a multiple-words named table. Perhaps that could be the reason of the problem? ;)

Situation

I have a fansite of a themepark and as you now, a themepark has many attractions. To ride an attraction, you must have a minimal height. Sometimes, small people can only ride it with an adult companion. But most of the time: you are allowed to ride the attraction because you just are tall enough ;)

Now I want to show the information of a specific attraction on my website. Name, content, photos, and so on. In addition to that information, I want to display my guests if they (or their kids) are tall enough to ride that attraction. It should appear like this way:

0m00 -> 1m00: not allowed
1m00 -> 1m30: allowed with an adult companion
1m30 -> 1m90: allowed

Database

I have two tables that are representing two objects: "attractions" & "attraction_accessibilities". In this case, I'm 100% sure the database names are correct.

Table "attraction_accessibilities" (id - name):

1 - Not allowed
2 - Allowed with an adult companion
3 - Allowed

Table "attractions" (id - name):

1 - Boomerang
2 - Huracan
3 - Los Piratas
4 - El Volador
...

Secondly, I should have another table between "attractions" and "attraction_accessibilities". This table should contain:

  • an id specific for each record
  • a link to the id of the "attractions" table (attraction_id)
  • a link to the id of the "attraction_accessibilities" table (attraction_accessibility_id)
  • the additional information like "min-height" and "max-height"

I think I should name that table "attraction_accessibilities_attractions". It's a constriction of the two other tables, and I did it that way because CakePHP proposed it when you're making a HABTM association (http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm).

But unfortunately, when I do call it that way, I've never succeeded to link those models in my application together.

Question

Is there anybody who've had the same problem but found a solution for it? If "yes": how should I name my database tables then and also important: how should I name my controller and model .php files?

Many thanks for the one who could help me and some other hopeless programmers ;)

Was it helpful?

Solution

If you use the HABTM relationship with unique set to keepExisting then you can name the table as you like and set the joinTable parameter according to he name you choosed

i.e. in your Attraction model

public $hasAndBelongsToMany = array(
    'AttractionAccessibility' =>
        array(
            'joinTable' => 'attraction_accessibilities_attractions',
        )
);

Instead if you're going to use the hasMany through relation then you can name the table as you like. In fact the so called "hasMany through" is just the concatenation of two hasMany relationships

so. If you name your table restrictions then

class Attraction extends AppModel {
    public $hasMany = array(
        'Restriction'
    );
}


class AttractionAccessibility extends AppModel {
    public $hasMany = array(
        'Restriction'
    );
}



class Restrictionextends AppModel {
    public $belongsTo = array(
        'Attraction ', 'AttractionAccessibility '
    );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top