Add prefix to (classes) table/entity names#276
Conversation
src/Shell/Task/ModelTask.php
Outdated
| } | ||
|
|
||
| if (!empty($this->params['model-prefix'])) { | ||
| $className = $this->_camelize($this->params['model-prefix']) . $className; |
There was a problem hiding this comment.
Spaces must be used to indent lines; tabs are not allowed
src/Shell/Task/ModelTask.php
Outdated
| } | ||
|
|
||
| if (!empty($this->params['model-prefix'])) { | ||
| $className = $this->_camelize($this->params['model-prefix']) . $className; |
There was a problem hiding this comment.
Spaces must be used to indent lines; tabs are not allowed
|
Wouldn't |
|
@dereuromark This only adds a prefix to the files/classes not the database table itself. it adds the prefix to both of the model parts. Table and Entity. example: Table in database is 'accessTokens' Model/Table/OauthAccessTokensTableTest.php Not sure if --table-prefix is a correct name. Sounds more like a prefix to the table in the mysql database. |
|
IMO it should be the other way around. Why would you want it the other way around? That is where plugins and namespaces come into play. |
|
I see there is an other pull request open for other way around. I think making plugins for every database will be a little overkill and models are in a lot of different folders. We have 2 databases with table 'products' in it. Thats why we have to prefix the class names. |
|
Just to clarify. You have two databases with tables of the same name. So you want to differentiate between them? I think using two plugins would be a better approach. A plugin for each database. |
|
@blieb sorry, just read your last comment. I think prefixing table classes conflicts with cakephp's naming conventions. |
|
Most importantly you will need to different DB connections here. |
|
We have a big project. Putting everything in a single database will be a mess. We sell (our) products to our customers. Most of them are stores. One of the products is a webshop. (Today I shocked a little, we have 20+ databases that have to work with this project) I believe cakephp does not have a good solution with working with multiple databases. It is possible. But I have to prefix the classes to prevent name conflicts. |
|
you can set the prefix through $this->table('') within table classes I think. |
|
I already told him that solution above. |
|
@inoas i Dont have to prefix the tables in mysql but in cakephp to prevent classname conflicts. |
|
You want to use Namespaces for that. But honestly. Plugins will give you namespaces and are exactly what you are looking for IMHO. |
|
I am not saying this PR feature is bad btw...! |
markstory
left a comment
There was a problem hiding this comment.
This new feature will need some tests before it can be merged. If you need help with the tests let me know.
src/Shell/Task/ModelTask.php
Outdated
| return null; | ||
| } | ||
| $name = $this->_entityName($model->alias()); | ||
| $name = $this->_entityName((!empty($this->params['model-prefix'])?$this->_camelize($this->params['model-prefix']):'') . $model->alias()); |
There was a problem hiding this comment.
You'll need to add spaces around the operators here.
src/Shell/Task/ModelTask.php
Outdated
| $name = $model->alias(); | ||
| $entity = $this->_entityName($model->alias()); | ||
| $name = (!empty($this->params['model-prefix'])?$this->_camelize($this->params['model-prefix']):'') . $model->alias(); | ||
| $entity = $this->_entityName((!empty($this->params['model-prefix'])?$this->_camelize($this->params['model-prefix']):'') . $model->alias()); |
There was a problem hiding this comment.
Can this repeated logic be extracted into a method?
There was a problem hiding this comment.
Spaces also around operators, also.
|
the logic is now in apart function, and also added the spaces around operators. Also fixed the relationships. |
| * Get the prefix for model if exist. | ||
| * | ||
| * @return string | ||
| */ |
There was a problem hiding this comment.
But does it need to be part of the public API here i wonder? It is only used internally here, right?
There was a problem hiding this comment.
Oops need to be private? That's true, only used internally
There was a problem hiding this comment.
protected, and no need to add it to the task list below.
|
@markstory tried to create a test. But for some reason it lost my params during 'bakeTable'. Do I miss something? ` /** ` |
|
@blieb Odd that is how the other tests work too. |
| } | ||
|
|
||
|
|
||
| /** |
There was a problem hiding this comment.
Expected docblock to be aligned with code.
|
|
||
|
|
||
| /** | ||
| * test bake() with a -model-prefix param |
There was a problem hiding this comment.
Expected 6 space(s) before asterisk; 5 found
|
|
||
| /** | ||
| * test bake() with a -model-prefix param | ||
| * |
There was a problem hiding this comment.
Expected 6 space(s) before asterisk; 5 found
| /** | ||
| * test bake() with a -model-prefix param | ||
| * | ||
| * @return void |
There was a problem hiding this comment.
Expected 6 space(s) before asterisk; 5 found
| * test bake() with a -model-prefix param | ||
| * | ||
| * @return void | ||
| */ |
There was a problem hiding this comment.
Expected 6 space(s) before asterisk; 5 found
|
now I merged the master branch to my branch, now I have more unittests errors, also the master branch is failing the tests :( |
|
Actually, looks good. |
|
@dereuromark I've fixed the CS, missed a space. But https://ci.appveyor.com/project/cakephp/bake/build/1.0.421 also still fails? I don't see why. (and 2 errors I merged from the master branch) |
|
Appveyor always fails usually. Travis must be fine. |
|
Is this waiting for review? |
|
It is still waiting, but I think there are some problems with the tests. (Don't know why it is failing) I've decided in my project to move the database tables to different plugins instead of prefixing the model names. so if you think this is not needed you can also close this pull request. |
|
I see more benefit for allowing prefixed tables to be used without the prefix showing up (the opposite of this PR). That is also what most people so far saw lacking. |
|
Please see #302 for the PR that should be merged instead. It specifies the source table, and the rest follows the convention. |
We are working with multiple databases, to prevent name conflicts we have to prefix the names of the table/Entity classes.
I've added --model-prefix to add a prefix to the name of de classes.
example:
bin/cake bake model AccessTokens -c db_oauth --model-prefix oauth