Skip to content

Add prefix to (classes) table/entity names#276

Closed
blieb wants to merge 8 commits intocakephp:masterfrom
blieb:model-prefix
Closed

Add prefix to (classes) table/entity names#276
blieb wants to merge 8 commits intocakephp:masterfrom
blieb:model-prefix

Conversation

@blieb
Copy link
Copy Markdown

@blieb blieb commented Sep 19, 2016

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

}

if (!empty($this->params['model-prefix'])) {
$className = $this->_camelize($this->params['model-prefix']) . $className;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaces must be used to indent lines; tabs are not allowed

}

if (!empty($this->params['model-prefix'])) {
$className = $this->_camelize($this->params['model-prefix']) . $className;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaces must be used to indent lines; tabs are not allowed

@dereuromark
Copy link
Copy Markdown
Member

Wouldn't --table-prefix be the more appropriate them here? After all this should only be relevant for finding the prefixed table in the DB schema.

@blieb
Copy link
Copy Markdown
Author

blieb commented Sep 19, 2016

@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
Model/Entity/OauthAccessToken.php

Not sure if --table-prefix is a correct name. Sounds more like a prefix to the table in the mysql database.

@dereuromark
Copy link
Copy Markdown
Member

dereuromark commented Sep 19, 2016

IMO it should be the other way around.
If a DB (for whatever legacy reason) has prefixed tables like foo_users, bake would not find it properly.
A --table-prefix foo_ would then find it and bake it as Users table and User entity and just set the manual $this->table('foo_users').

Why would you want it the other way around? That is where plugins and namespaces come into play.

@blieb
Copy link
Copy Markdown
Author

blieb commented Sep 19, 2016

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.

@thinkingmedia
Copy link
Copy Markdown
Contributor

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.

@thinkingmedia
Copy link
Copy Markdown
Contributor

@blieb sorry, just read your last comment.

I think prefixing table classes conflicts with cakephp's naming conventions.

@dereuromark
Copy link
Copy Markdown
Member

Most importantly you will need to different DB connections here.

@blieb
Copy link
Copy Markdown
Author

blieb commented Sep 19, 2016

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.
So the second database will have products of the stores.

(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.

@inoas
Copy link
Copy Markdown
Contributor

inoas commented Sep 19, 2016

you can set the prefix through $this->table('') within table classes I think.

@dereuromark
Copy link
Copy Markdown
Member

I already told him that solution above.

@blieb
Copy link
Copy Markdown
Author

blieb commented Sep 19, 2016

@inoas i Dont have to prefix the tables in mysql but in cakephp to prevent classname conflicts.

@inoas
Copy link
Copy Markdown
Contributor

inoas commented Sep 19, 2016

You want to use Namespaces for that. But honestly. Plugins will give you namespaces and are exactly what you are looking for IMHO.

@markstory markstory added this to the 1.2.9 milestone Sep 19, 2016
@inoas
Copy link
Copy Markdown
Contributor

inoas commented Sep 19, 2016

I am not saying this PR feature is bad btw...!

Copy link
Copy Markdown
Member

@markstory markstory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new feature will need some tests before it can be merged. If you need help with the tests let me know.

return null;
}
$name = $this->_entityName($model->alias());
$name = $this->_entityName((!empty($this->params['model-prefix'])?$this->_camelize($this->params['model-prefix']):'') . $model->alias());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to add spaces around the operators here.

$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());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this repeated logic be extracted into a method?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaces also around operators, also.

@blieb
Copy link
Copy Markdown
Author

blieb commented Sep 21, 2016

the logic is now in apart function, and also added the spaces around operators.

Also fixed the relationships.
I have to jump in to the tests because I never created one yet :)

* Get the prefix for model if exist.
*
* @return string
*/
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But does it need to be part of the public API here i wonder? It is only used internally here, right?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops need to be private? That's true, only used internally

Copy link
Copy Markdown
Member

@dereuromark dereuromark Sep 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protected, and no need to add it to the task list below.

@blieb
Copy link
Copy Markdown
Author

blieb commented Oct 3, 2016

@markstory tried to create a test. But for some reason it lost my params during 'bakeTable'. Do I miss something?

` /**
* test bake() with a -model-prefix param
*
* @return void
*/
public function testBakeTableWithModelPrefix()
{

    $this->Task->params['model-prefix'] = 'test';

    $filename = $this->_normalizePath(APP . 'Model/Table/TestBakeArticlesTable.php');
    $this->Task->expects($this->once())->method('createFile')
        ->with($filename, $this->stringContains('class TestBakeArticlesTable extends'));


    $model = TableRegistry::get('BakeArticles');
    $result = $this->Task->bakeTable($model);
    $this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

`

@markstory
Copy link
Copy Markdown
Member

@blieb Odd that is how the other tests work too.

}


/**
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected docblock to be aligned with code.



/**
* test bake() with a -model-prefix param
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected 6 space(s) before asterisk; 5 found


/**
* test bake() with a -model-prefix param
*
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected 6 space(s) before asterisk; 5 found

/**
* test bake() with a -model-prefix param
*
* @return void
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected 6 space(s) before asterisk; 5 found

* test bake() with a -model-prefix param
*
* @return void
*/
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected 6 space(s) before asterisk; 5 found

@blieb
Copy link
Copy Markdown
Author

blieb commented Oct 25, 2016

now I merged the master branch to my branch, now I have more unittests errors, also the master branch is failing the tests :(

@dereuromark
Copy link
Copy Markdown
Member

Actually, looks good.
You just have to fix your CS errors: https://travis-ci.org/cakephp/bake/jobs/170403000

@blieb
Copy link
Copy Markdown
Author

blieb commented Oct 25, 2016

@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)

@dereuromark
Copy link
Copy Markdown
Member

Appveyor always fails usually. Travis must be fine.

@markstory markstory self-assigned this Oct 25, 2016
@lorenzo
Copy link
Copy Markdown
Member

lorenzo commented Nov 15, 2016

Is this waiting for review?

@blieb
Copy link
Copy Markdown
Author

blieb commented Nov 15, 2016

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.

@dereuromark
Copy link
Copy Markdown
Member

dereuromark commented Nov 15, 2016

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.
👎 for continuing this one.

@markstory markstory removed their assignment Nov 24, 2016
@dereuromark
Copy link
Copy Markdown
Member

Please see #302 for the PR that should be merged instead. It specifies the source table, and the rest follows the convention.

@markstory markstory closed this Jan 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants