Skip to content

Commit e249db0

Browse files
ISSUE-1317: Fix model --force creation bugs
1 parent e75d332 commit e249db0

File tree

3 files changed

+98
-115
lines changed

3 files changed

+98
-115
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- Fixed wrong request filtering [#1468](https://github.com/phalcon/phalcon-devtools/issues/1468)
77
- Fixed empty namespace generation [#1467](https://github.com/phalcon/phalcon-devtools/issues/1467)
88
- Removed `model->getSource()` method generation due to its becoming final in `Phalcon\Mvc\Model` [#1297](https://github.com/phalcon/phalcon-devtools/issues/1297)
9+
- Fixed model `--force` creation bugs [#1317](https://github.com/phalcon/phalcon-devtools/issues/1317)
10+
- Fixed mapping of PascalCase table fields [#1463](https://github.com/phalcon/phalcon-devtools/issues/1463)
911

1012

1113
# [4.0.5](https://github.com/phalcon/cphalcon/releases/tag/v4.0.5) (2021-03-14)

src/Builder/Component/AllModels.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,9 @@ public function build(): void
8585
$genSettersGetters = $this->options->get('genSettersGetters', false);
8686
$mapColumn = $this->options->get('mapColumn', false);
8787

88-
$adapter = $config->database->adapter;
88+
$adapter = $config->database->adapter ?? 'Mysql';
8989
$this->isSupportedAdapter($adapter);
9090

91-
$adapter = 'Mysql';
92-
if (isset($config->database->adapter)) {
93-
$adapter = $config->database->adapter;
94-
}
95-
9691
if (is_object($config->database)) {
9792
$configArray = $config->database->toArray();
9893
} else {

src/Builder/Component/Model.php

Lines changed: 95 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use Phalcon\Validation;
2828
use Phalcon\Validation\Validator\Email as EmailValidator;
2929
use ReflectionClass;
30+
use ReflectionClassConstant;
31+
use ReflectionProperty;
3032

3133
/**
3234
* Builder to generate models
@@ -121,14 +123,9 @@ public function build(): void
121123
$genDocMethods = $this->modelOptions->getValidOptionOrDefault('genDocMethods', false);
122124
$useSettersGetters = $this->modelOptions->getValidOptionOrDefault('genSettersGetters', false);
123125

124-
$adapter = $config->database->adapter;
126+
$adapter = $config->database->adapter ?? 'Mysql';
125127
$this->isSupportedAdapter($adapter);
126128

127-
$adapter = 'Mysql';
128-
if (isset($config->database->adapter)) {
129-
$adapter = $config->database->adapter;
130-
}
131-
132129
if (is_object($config->database)) {
133130
$configArray = $config->database->toArray();
134131
} else {
@@ -168,40 +165,36 @@ public function build(): void
168165

169166
foreach ($referenceList as $tableName => $references) {
170167
foreach ($references as $reference) {
171-
if ($reference->getReferencedTable() != $this->modelOptions->getOption('name')) {
168+
if ($reference->getReferencedTable() !== $this->modelOptions->getOption('name')) {
172169
continue;
173170
}
174171

175-
$entityNamespace = '';
176-
if ($this->modelOptions->hasOption('namespace')) {
177-
$entityNamespace = $this->modelOptions->getOption('namespace')."\\";
178-
}
172+
$entityNamespace = $this->modelOptions->hasOption('namespace')
173+
? $this->modelOptions->getOption('namespace')."\\" : '';
179174

180175
$refColumns = $reference->getReferencedColumns();
181176
$columns = $reference->getColumns();
182177
$initialize[] = $snippet->getRelation(
183178
'hasMany',
184-
$this->modelOptions->getOption('camelize') ? Utils::lowerCamelize($refColumns[0]) : $refColumns[0],
179+
$this->getFieldName($refColumns[0]),
185180
$entityNamespace . Text::camelize($tableName, '_-'),
186-
$this->modelOptions->getOption('camelize') ? Utils::lowerCamelize($columns[0]) : $columns[0],
181+
$this->getFieldName($columns[0]),
187182
"['alias' => '" . Text::camelize($tableName, '_-') . "']"
188183
);
189184
}
190185
}
191186

192187
foreach ($db->describeReferences($this->modelOptions->getOption('name'), $schema) as $reference) {
193-
$entityNamespace = '';
194-
if ($this->modelOptions->hasOption('namespace')) {
195-
$entityNamespace = $this->modelOptions->getOption('namespace');
196-
}
188+
$entityNamespace = $this->modelOptions->hasOption('namespace')
189+
? $this->modelOptions->getOption('namespace') : '';
197190

198191
$refColumns = $reference->getReferencedColumns();
199192
$columns = $reference->getColumns();
200193
$initialize[] = $snippet->getRelation(
201194
'belongsTo',
202-
$this->modelOptions->getOption('camelize') ? Utils::lowerCamelize($columns[0]) : $columns[0],
195+
$this->getFieldName($columns[0]),
203196
$this->getEntityClassName($reference, $entityNamespace),
204-
$this->modelOptions->getOption('camelize') ? Utils::lowerCamelize($refColumns[0]) : $refColumns[0],
197+
$this->getFieldName($refColumns[0]),
205198
"['alias' => '" . Text::camelize($reference->getReferencedTable(), '_-') . "']"
206199
);
207200
}
@@ -236,7 +229,7 @@ public function build(): void
236229
}
237230
$reflection = new ReflectionClass($fullClassName);
238231
foreach ($reflection->getMethods() as $method) {
239-
if ($method->getDeclaringClass()->getName() != $fullClassName) {
232+
if ($method->getDeclaringClass()->getName() !== $fullClassName) {
240233
continue;
241234
}
242235

@@ -284,117 +277,60 @@ public function build(): void
284277
}
285278
}
286279

287-
$possibleFields = $possibleFieldsTransformed = [];
280+
$possibleFieldsTransformed = [];
288281
foreach ($fields as $field) {
289-
$possibleFields[$field->getName()] = true;
290-
if ($this->modelOptions->getOption('camelize')) {
291-
$fieldName = Utils::lowerCamelize(Utils::camelize($field->getName(), '_-'));
292-
} else {
293-
$fieldName = Utils::lowerCamelize(Utils::camelize($field->getName(), '-'));
294-
}
282+
$fieldName = $this->getFieldName($field->getName());
295283
$possibleFieldsTransformed[$fieldName] = true;
296284
}
297285

298286
if (method_exists($reflection, 'getReflectionConstants')) {
299287
foreach ($reflection->getReflectionConstants() as $constant) {
300-
if ($constant->getDeclaringClass()->getName() != $fullClassName) {
288+
if ($constant->getDeclaringClass()->getName() !== $fullClassName) {
301289
continue;
302290
}
303-
$constantsPreg = '/^(\s*)const(\s+)'.$constant->getName().'([\s=;]+)/';
304-
$endLine = $startLine = 0;
305-
foreach ($linesCode as $line => $code) {
306-
if (preg_match($constantsPreg, $code)) {
307-
$startLine = $line;
308-
break;
309-
}
310-
}
311-
if (!empty($startLine)) {
312-
$countLines = count($linesCode);
313-
for ($i = $startLine; $i < $countLines; $i++) {
314-
if (preg_match('/;(\s*)$/', $linesCode[$i])) {
315-
$endLine = $i;
316-
break;
317-
}
318-
}
319-
}
320291

321-
if (!empty($startLine) && !empty($endLine)) {
322-
$constantDeclaration = join(
323-
'',
324-
array_slice(
325-
$linesCode,
326-
$startLine,
327-
$endLine - $startLine + 1
328-
)
329-
);
330-
$attributes[] = PHP_EOL . " " . $constant->getDocComment() .
331-
PHP_EOL . $constantDeclaration;
292+
$constantsPreg = '/const(\s+)' . $constant->getName() . '([\s=;]+)/';
293+
$attribute = $this->getAttribute($linesCode, $constantsPreg, $constant);
294+
if (!empty($attribute)) {
295+
$attributes[] = $attribute;
332296
}
333297
}
334298
}
335299

336300
foreach ($reflection->getProperties() as $property) {
337301
$propertyName = $property->getName();
338-
/** @var null|string $possibleFieldsValue */
339-
$possibleFieldsValue = $possibleFieldsTransformed[$propertyName];
340-
341-
if ($property->getDeclaringClass()->getName() != $fullClassName ||
342-
!empty($possibleFieldsValue)) {
302+
if (!empty($possibleFieldsTransformed[$propertyName])
303+
|| $property->getDeclaringClass()->getName() !== $fullClassName
304+
) {
343305
continue;
344306
}
345307

346308
$modifiersPreg = '';
347309
switch ($property->getModifiers()) {
348-
case \ReflectionProperty::IS_PUBLIC:
310+
case ReflectionProperty::IS_PUBLIC:
349311
$modifiersPreg = '^(\s*)public(\s+)';
350312
break;
351-
case \ReflectionProperty::IS_PRIVATE:
313+
case ReflectionProperty::IS_PRIVATE:
352314
$modifiersPreg = '^(\s*)private(\s+)';
353315
break;
354-
case \ReflectionProperty::IS_PROTECTED:
316+
case ReflectionProperty::IS_PROTECTED:
355317
$modifiersPreg = '^(\s*)protected(\s+)';
356318
break;
357-
case \ReflectionProperty::IS_STATIC + \ReflectionProperty::IS_PUBLIC:
319+
case ReflectionProperty::IS_STATIC + ReflectionProperty::IS_PUBLIC:
358320
$modifiersPreg = '^(\s*)(public?)(\s+)static(\s+)';
359321
break;
360-
case \ReflectionProperty::IS_STATIC + \ReflectionProperty::IS_PROTECTED:
322+
case ReflectionProperty::IS_STATIC + ReflectionProperty::IS_PROTECTED:
361323
$modifiersPreg = '^(\s*)protected(\s+)static(\s+)';
362324
break;
363-
case \ReflectionProperty::IS_STATIC + \ReflectionProperty::IS_PRIVATE:
325+
case ReflectionProperty::IS_STATIC + ReflectionProperty::IS_PRIVATE:
364326
$modifiersPreg = '^(\s*)private(\s+)static(\s+)';
365327
break;
366328
}
367329

368330
$modifiersPreg = '/' . $modifiersPreg . '\$' . $propertyName . '([\s=;]+)/';
369-
$endLine = $startLine = 0;
370-
foreach ($linesCode as $line => $code) {
371-
if (preg_match($modifiersPreg, $code)) {
372-
$startLine = $line;
373-
break;
374-
}
375-
}
376-
377-
if (!empty($startLine)) {
378-
$countLines = count($linesCode);
379-
for ($i = $startLine; $i < $countLines; $i++) {
380-
if (preg_match('/;(\s*)$/', $linesCode[$i])) {
381-
$endLine = $i;
382-
break;
383-
}
384-
}
385-
}
386-
387-
if (!empty($startLine) && !empty($endLine)) {
388-
$propertyDeclaration = join(
389-
'',
390-
array_slice(
391-
$linesCode,
392-
$startLine,
393-
$endLine - $startLine + 1
394-
)
395-
);
396-
$attributes[] = PHP_EOL . " " . $property->getDocComment() . PHP_EOL .
397-
$propertyDeclaration;
331+
$attribute = $this->getAttribute($linesCode, $modifiersPreg, $property);
332+
if (!empty($attribute)) {
333+
$attributes[] = $attribute;
398334
}
399335
}
400336
} catch (\Exception $e) {
@@ -410,12 +346,9 @@ public function build(): void
410346

411347
$validations = [];
412348
foreach ($fields as $field) {
349+
$fieldName = $this->getFieldName($field->getName());
350+
413351
if ($field->getType() === Column::TYPE_CHAR) {
414-
if ($this->modelOptions->getOption('camelize')) {
415-
$fieldName = Utils::lowerCamelize(Utils::camelize($field->getName(), '_-'));
416-
} else {
417-
$fieldName = Utils::lowerCamelize(Utils::camelize($field->getName(), '-'));
418-
}
419352
$domain = [];
420353
if (preg_match('/\((.*)\)/', (string)$field->getType(), $matches)) {
421354
foreach (explode(',', $matches[1]) as $item) {
@@ -428,12 +361,7 @@ public function build(): void
428361
}
429362
}
430363

431-
if ($field->getName() == 'email') {
432-
if ($this->modelOptions->getOption('camelize')) {
433-
$fieldName = Utils::lowerCamelize(Utils::camelize($field->getName(), '_-'));
434-
} else {
435-
$fieldName = Utils::lowerCamelize(Utils::camelize($field->getName(), '-'));
436-
}
364+
if ($field->getName() === 'email') {
437365
$validations[] = $snippet->getValidateEmail($fieldName);
438366
$uses[] = $snippet->getUseAs(EmailValidator::class, 'EmailValidator');
439367
}
@@ -465,8 +393,7 @@ public function build(): void
465393
}
466394

467395
$type = $this->getPHPType($field->getType());
468-
$fieldName = Utils::lowerCamelizeWithDelimiter($field->getName(), '-', true);
469-
$fieldName = $this->modelOptions->getOption('camelize') ? Utils::lowerCamelize($fieldName) : $fieldName;
396+
$fieldName = $this->getFieldName($field->getName());
470397
$attributes[] = $snippet->getAttributes(
471398
$type,
472399
$useSettersGetters ? 'protected' : 'public',
@@ -571,6 +498,65 @@ public function build(): void
571498
}
572499
}
573500

501+
/**
502+
* @param array $linesCode
503+
* @param string $pattern
504+
* @param ReflectionProperty|ReflectionClassConstant $attribute
505+
*
506+
* @return null|string
507+
*/
508+
protected function getAttribute(array $linesCode, string $pattern, $attribute): ?string
509+
{
510+
$endLine = $startLine = 0;
511+
foreach ($linesCode as $line => $code) {
512+
if (preg_match($pattern, $code)) {
513+
$startLine = $line;
514+
break;
515+
}
516+
}
517+
if (!empty($startLine)) {
518+
$countLines = count($linesCode);
519+
for ($i = $startLine; $i < $countLines; $i++) {
520+
if (preg_match('/;(\s*)$/', $linesCode[$i])) {
521+
$endLine = $i;
522+
break;
523+
}
524+
}
525+
}
526+
527+
if (!empty($startLine) && !empty($endLine)) {
528+
$attributeDeclaration = join(
529+
'',
530+
array_slice(
531+
$linesCode,
532+
$startLine,
533+
$endLine - $startLine + 1
534+
)
535+
);
536+
$attributeFormatted = $attributeDeclaration;
537+
if (!empty($attribute->getDocComment())) {
538+
$attributeFormatted = " " . $attribute->getDocComment() . PHP_EOL . $attribute;
539+
}
540+
return $attributeFormatted;
541+
}
542+
543+
return null;
544+
}
545+
546+
/**
547+
* @param string $fieldName
548+
*
549+
* @return string
550+
*/
551+
protected function getFieldName(string $fieldName): string
552+
{
553+
if ($this->modelOptions->getOption('camelize')) {
554+
return Utils::lowerCamelize(Utils::camelize($fieldName, '_-'));
555+
}
556+
557+
return Utils::lowerCamelizeWithDelimiter($fieldName, '-', true);
558+
}
559+
574560
/**
575561
* Set path to model
576562
*

0 commit comments

Comments
 (0)