-
-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong capitals for class/files when table is uppercase #366
Comments
How would it work with |
Not sure what you mean but I would just change the generated class and file names. The table set in getTableName would not change. I have this working for MSSQL and MySQL (I extended the Generator). On the other hand, I must to say that I am not familiar with Postgre. |
I mean expected result for |
AFAICS, it would not change. The change would affect only |
How would you implement that? |
// I changed this in generateClassnames()
return $this->classNames[$fullTableName] = Inflector::id2camel($schemaName.$className, '_');
// For this (not beauty but works for my companie DBs [mysql, mssql])
return $this->classNames[$fullTableName] = strtr(ucwords(implode(' ', explode('_', strtolower($schemaName.$className)))), [' ' => '']);
// To implement it for MyTable I just would use Inflector::camel2words()
// (preferably in a bit more clean way than this)
return $this->classNames[$fullTableName] = strtr(ucwords(implode(' ', explode('_', strtolower(strtr(Inflector::camel2words($schemaName.$className), [' ' => '_']))))), [' ' => '']); |
It won't work: https://3v4l.org/164si |
May be your test function public static function camel2words($name, $ucwords = true)
{
$label = strtolower(trim(str_replace([
'-',
'_',
'.',
], ' ', preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $name))));
return $ucwords ? ucwords($label) : $label;
} |
Just tested here breaking it in parts: $data = [
'MY_TABLE' => 'MyTable',
'MyTable' => 'MyTable',
'my_table' => 'MyTable',
'myTable' => 'MyTable',
];
// strtr(ucwords(implode(' ', explode('_', strtolower(strtr(Inflector::camel2words($schemaName.$className), [' ' => '_']))))), [' ' => '']);
function genNames($input) {
$cc = Inflector::camel2words($input);
$cca = strtr($cc, [' '=>'_']);
$ccal = strtolower($cca);
$ccala = explode('_', $ccal);
$ccalai = implode(' ', $ccala);
$ccalaiw = ucwords($ccalai);
$final = strtr($ccalaiw, [' '=>'']);
return $final;
}
$result = '';
foreach ($data as $orig => $expected) {
$finalName = genNames($orig);
$result .= $orig . ' would be changed to ' . $finalName . '<hr>';
}
return $result; Result:
|
OK. That looks like it. Do you want to make a pull request? |
Yeah, I am on it. I would already do but i had no time this weekend. I'll do this week. 👍 |
@samdark, should I create some tables with variated names (underlines, hyphens, uppers, etc) inside |
I think I found a better approach: it would be to mock |
Yep, mock is better. |
I did just basic tests for that specific method. So I just turned that method public to test against it. I know it is not the preffered way for tests but I hope it to be enough. If not, let me know. |
This last update broke my application's ability to update the existing model files with old case, and instead it creates new models of a new filename and new classname. How do I configure Gii to revert to the previous functionality and preserve the DB table names as classnames and filenames? My DB table name is ABBRMyTable on purpose. My expected Model class name and filename is ABBRMyTable, i.e. no change, but I get AbbrmyTable. Another example is MyTableABBR, and I expected no change in the case, but I am getting MyTableAbbr. Let me know how to resolve soon, so I can continue using Gii for model regeneration. |
Models' names should be PSR compliant. That is why this change was done. For your specific case (and may others), it could be implemented a new attribute to BTW, keep in mind that Gii should be used primarily to "bootstrap" your models' code. |
Yes, that could be a problem. Technically it's a breaking change so either next version should be major or we should make it configurable and not turned on by default. |
Thanks for considering making this a configurable option in Gii to allow for no change in the case.
Regarding the typical usage of Gii for bootstrapping models, our organization has a non-typical usage, borrowed from other language ORMs. We extended the Gii Model Generator to separate models and base models. The base model is continuously generated for modified columns and relationships after DB migrations, and the application logic lives in the models that are not regenerated. In this usage, I cannot easily copy the preview to a different file name but prefer to override the existing base model file.
|
@samdark, I can do it too, no problem. What about a bool form attribute like |
It should not be exact name, just the same way it was before and yes, it should be default. You could work on it. |
PR done. Not sure if the name The feature is now optional and the checkbox is not even displayed in the form except when table name contains *. I also included a hint for the field. |
* upstream/master: Remove useless import of `Yii` from CRUD generator search model template Fixes yiisoft#379: Fixed bug in view page where delete button not work well Fixes yiisoft#366: Option to allow standardized class names capitals in model generator Fixes yiisoft#327: Fixed bug in Model generator when $baseClass is an abstract class Fixes yiisoft#366: Better class and file names for uppercase tables Update composer.json (yiisoft#364) Fix codestyle (yiisoft#362) prepare for next release release version 2.0.7 Register CSRF meta-tags dynamically Updated CHANGELOG [ci skip] Fixed rules generation for JSON columns Removed redundant line from license [skip ci] added database version to issue template (yiisoft#352) [skip ci] docs/guide-ja revised [ci skip] (yiisoft#349) Updated issue template [skip ci] Fixes yiisoft#185: Fix bug in Model generators when FKs pointing to non-existing tables Fixes yiisoft#340: Fix bug in CRUD SearchModel generator Require Yii 2.0.14 (yiisoft#339) Conflicts: composer.json
What steps will reproduce the problem?
yiisoft/yii2-gii uses
BaseInflector::id2camel()
to generate class names but, when getting upper case table names from db likeMY_TABLE
, id2camel() does not work properly.If it is ok, I could do a PR removing id2camel() and doing an inline conversion inside
Generator::generateClassNames()
.What is the expected result?
What do you get instead?
Additional info
The text was updated successfully, but these errors were encountered: