📜 ⬆️ ⬇️

Cakephp HABTM auto-build models

Such an unpleasant thing as uto-create a model appeared in the keycake.
On the one hand, it is certainly useful, but on the other hand it’s very difficult to live.
For example, your linking plate should perform not only the function of communication, but also carry some functionality.
This unpleasant incident just happened to me. The project was written on the beta version of Keyk and now the task has become to deliver the release.
Let us take an example of what is happening.

may we have three signs

- users
list of users;
')
- teams
teams (well, like football teams)

- teammates
sign connecting users and teams.
besides the main fields id, user_id, team_id there will be additional fields in it,
for example, such as the user’s name in the command, his number, the date the user was added to the command, and the delete flag from the command.

For each label we create the appropriate model: User, Team, Teammate.
Now we describe the connections for the Team model.
var $ hasAndBelongsToMany = array (
'User' => array ('className' => 'User',
'joinTable' => 'teammates',
'foreignKey' => 'team_id',
'associationForeignKey' => 'user_id',
'unique' => true
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
for teammate model
var $ belongsTo = array (
'User' => array ('className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Team' => array ('className' => 'Team',
'foreignKey' => 'team_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
In addition, in the Teammate model, we will create several methods for working with it. well, for example test () and test1 ()
Now in the TeamsController controller, we will connect the Teammate model and in any action we will do pr ($ this-> Teammate).
We will see that our Teammate model does not have the properties that we endowed it with.
I killed a lot of time, until I realized what was going on. And the following happens:
Uto-create a model was added to the keik's release (http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM)
Now, a smart cake sees that there is a HABTM and, by the joinTable, creates a model (in our case, Teammate) and grinds the existing one,
the fact that such a model already has it for some reason does not care at all.
Then I saw the parameter with for HABTM - Defines the join table.
By default CakePHP will auto-create a model for you.
Using the example above it would be called RecipesTag.
By using this key you can override this default name.
It is possible to use the join table directly.
Hmm, well, I thought. And if I didn’t use this parameter, I wrote 'with' => 'TeamsUsers', but Kake decided that there was a model,
and there are no labels, and again, the label is described in the joinTable does not bother him either.
At the moment I found the only way out:
rename teammates table to teams_users, teammate table $ useTable = "teams_users" ;, 'joinTable' => 'teams_users' parameter

If someone has other ideas, share how to turn off auto-creation of models.

Source: https://habr.com/ru/post/52341/


All Articles