| @@ 340-425 (lines=86) @@ | ||
| 337 | * |
|
| 338 | * @return $this |
|
| 339 | */ |
|
| 340 | public function replaceRelationshipTables() { |
|
| 341 | ||
| 342 | $functions = ""; |
|
| 343 | $functionsCall = ""; |
|
| 344 | $removeAll = ""; |
|
| 345 | $removeAllCall = ""; |
|
| 346 | $includes = ""; |
|
| 347 | $joins = ""; |
|
| 348 | $joinSorts = ""; |
|
| 349 | ||
| 350 | $method = File::get($this->stubsDirectory . '/Controller/ControllerRelationshipTable.php'); |
|
| 351 | ||
| 352 | foreach ($this->modelData->reverseRelationships as $relationship) { |
|
| 353 | ||
| 354 | if ($relationship->type == "belongsToMany") { |
|
| 355 | $relatedTablePluralized = CamelCase::pluralize($relationship->relatedTable); |
|
| 356 | $relatedTablePluralizedUc = CamelCase::pluralize(CamelCase::convertToCamelCase($relationship->relatedTable)); |
|
| 357 | ||
| 358 | $replacedMethod = ''; |
|
| 359 | $replacedMethod = str_replace('{{related_table_pl_uc}}', $relatedTablePluralizedUc, $method); |
|
| 360 | $replacedMethod = str_replace('{{class_name_lw}}', $this->modelData->tableName, $replacedMethod); |
|
| 361 | $replacedMethod = str_replace('{{related_table_pl}}', $relatedTablePluralized, $replacedMethod); |
|
| 362 | $replacedMethod = str_replace('{{foreign_key}}', $relationship->foreignKey, $replacedMethod); |
|
| 363 | $replacedMethod = str_replace('{{related_field}}', $relationship->relatedField, $replacedMethod); |
|
| 364 | $replacedMethod = str_replace('{{foreign_table_lw}}', strtolower($relationship->modelName), $replacedMethod); |
|
| 365 | $replacedMethod = str_replace('{{foreign_table}}', $relationship->modelName, $replacedMethod); |
|
| 366 | ||
| 367 | $functions .= $replacedMethod; |
|
| 368 | ||
| 369 | $methodCall = 'if (array_key_exists(\'{{related_table_pl}}\', $vars))'; |
|
| 370 | $methodCall .= "\n\t\t\t"; |
|
| 371 | $methodCall .= '$this->save{{related_table_pl_uc}}($vars, ${{class_name_lw}});'; |
|
| 372 | $methodCall = str_replace('{{related_table_pl_uc}}', $relatedTablePluralizedUc, $methodCall); |
|
| 373 | $methodCall = str_replace('{{related_table_pl}}', $relatedTablePluralized, $methodCall); |
|
| 374 | $methodCall = str_replace('{{class_name_lw}}', $this->modelData->tableName, $methodCall); |
|
| 375 | ||
| 376 | $functionsCall .= $methodCall . "\n\t\t"; |
|
| 377 | ||
| 378 | $removeAllMethod = File::get($this->stubsDirectory . '/Controller/ControllerRemoveAll.php'); |
|
| 379 | $removeAllMethod = str_replace('{{related_table_pl_uc}}', $relatedTablePluralizedUc, $removeAllMethod); |
|
| 380 | $removeAllMethod = str_replace('{{foreign_key}}', $relationship->foreignKey, $removeAllMethod); |
|
| 381 | $removeAllMethod = str_replace('{{foreign_table}}', $relationship->modelName, $removeAllMethod); |
|
| 382 | $removeAllMethod = str_replace('{{foreign_table_lw_pl}}', CamelCase::pluralize(strtolower($relationship->modelName)), $removeAllMethod); |
|
| 383 | ||
| 384 | $removeAll .= $removeAllMethod; |
|
| 385 | ||
| 386 | $removeAllCallMethod = '$this->deleteAll{{related_table_pl_uc}}(${{class_name_lw}}[\'id\']);'; |
|
| 387 | $removeAllCallMethod = str_replace('{{related_table_pl_uc}}', $relatedTablePluralizedUc, $removeAllCallMethod); |
|
| 388 | $removeAllCallMethod = str_replace('{{class_name_lw}}', $this->modelData->tableName, $removeAllCallMethod); |
|
| 389 | ||
| 390 | $removeAllCall .= $removeAllCallMethod . "\n\t\t"; |
|
| 391 | ||
| 392 | $joinRelationshipTableStub = File::get($this->stubsDirectory . 'SearchConditions/joinRelationshipTable.php'); |
|
| 393 | $joinRelationshipTableStub = str_replace('{{class_name_lw}}', $this->modelData->tableName, $joinRelationshipTableStub); |
|
| 394 | $joinRelationshipTableStub = str_replace('{{related_table_pl}}', $relatedTablePluralized, $joinRelationshipTableStub); |
|
| 395 | $joinRelationshipTableStub = str_replace('{{related_table}}', CamelCase::convertToCamelCase($relationship->relatedTable), $joinRelationshipTableStub); |
|
| 396 | $joinRelationshipTableStub = str_replace('{{foreign_key}}', $relationship->foreignKey, $joinRelationshipTableStub); |
|
| 397 | $joinRelationshipTableStub = str_replace('{{related_field}}', $relationship->relatedField, $joinRelationshipTableStub); |
|
| 398 | $joinRelationshipTableStub = str_replace('{{foreign_table}}', $relationship->tableName, $joinRelationshipTableStub); |
|
| 399 | ||
| 400 | $joins .= $joinRelationshipTableStub . "\n"; |
|
| 401 | ||
| 402 | $use = 'use App\Models\{{foreign_table}};'; |
|
| 403 | $use = str_replace('{{foreign_table}}', $relationship->modelName, $use); |
|
| 404 | ||
| 405 | $includes .= $use . "\n"; |
|
| 406 | } |
|
| 407 | } |
|
| 408 | ||
| 409 | $this->stub = str_replace('{{relationship_tables_store}}', $functions, $this->stub); |
|
| 410 | ||
| 411 | $this->stub = str_replace('{{relationship_tables_call}}', $functionsCall, $this->stub); |
|
| 412 | ||
| 413 | $this->stub = str_replace('{{remove_relationship_objects}}', $removeAll, $this->stub); |
|
| 414 | ||
| 415 | $this->stub = str_replace('{{remove_relationship_objects_call}}', $removeAllCall, $this->stub); |
|
| 416 | ||
| 417 | $this->stub = str_replace('{{relationship_tables_classes}}', $includes, $this->stub); |
|
| 418 | ||
| 419 | $this->stub = str_replace('{{relationship_tables_joins}}', $joins, $this->stub); |
|
| 420 | ||
| 421 | ||
| 422 | ||
| 423 | return $this; |
|
| 424 | ||
| 425 | } |
|
| 426 | ||
| 427 | ||
| 428 | /** |
|
| @@ 393-478 (lines=86) @@ | ||
| 390 | * |
|
| 391 | * @return $this |
|
| 392 | */ |
|
| 393 | public function replaceRelationshipTables() { |
|
| 394 | ||
| 395 | $functions = ""; |
|
| 396 | $functionsCall = ""; |
|
| 397 | $removeAll = ""; |
|
| 398 | $removeAllCall = ""; |
|
| 399 | $includes = ""; |
|
| 400 | $joins = ""; |
|
| 401 | $joinSorts = ""; |
|
| 402 | ||
| 403 | $method = File::get($this->stubsDirectory . '/Model/ModelRelationshipTable.php'); |
|
| 404 | ||
| 405 | foreach ($this->modelData->reverseRelationships as $relationship) { |
|
| 406 | ||
| 407 | if ($relationship->type == "belongsToMany") { |
|
| 408 | $relatedTablePluralized = CamelCase::pluralize($relationship->relatedTable); |
|
| 409 | $relatedTablePluralizedUc = CamelCase::pluralize(CamelCase::convertToCamelCase($relationship->relatedTable)); |
|
| 410 | ||
| 411 | $replacedMethod = ''; |
|
| 412 | $replacedMethod = str_replace('{{related_table_pl_uc}}', $relatedTablePluralizedUc, $method); |
|
| 413 | $replacedMethod = str_replace('{{class_name_lw}}', $this->modelData->tableName, $replacedMethod); |
|
| 414 | $replacedMethod = str_replace('{{related_table_pl}}', $relatedTablePluralized, $replacedMethod); |
|
| 415 | $replacedMethod = str_replace('{{foreign_key}}', $relationship->foreignKey, $replacedMethod); |
|
| 416 | $replacedMethod = str_replace('{{related_field}}', $relationship->relatedField, $replacedMethod); |
|
| 417 | $replacedMethod = str_replace('{{foreign_table_lw}}', strtolower($relationship->modelName), $replacedMethod); |
|
| 418 | $replacedMethod = str_replace('{{foreign_table}}', $relationship->modelName, $replacedMethod); |
|
| 419 | ||
| 420 | $functions .= $replacedMethod; |
|
| 421 | ||
| 422 | $methodCall = 'if (array_key_exists(\'{{related_table_pl}}\', $vars))'; |
|
| 423 | $methodCall .= "\n\t\t\t"; |
|
| 424 | $methodCall .= '$this->save{{related_table_pl_uc}}($vars, ${{class_name_lw}});'; |
|
| 425 | $methodCall = str_replace('{{related_table_pl_uc}}', $relatedTablePluralizedUc, $methodCall); |
|
| 426 | $methodCall = str_replace('{{related_table_pl}}', $relatedTablePluralized, $methodCall); |
|
| 427 | $methodCall = str_replace('{{class_name_lw}}', $this->modelData->tableName, $methodCall); |
|
| 428 | ||
| 429 | $functionsCall .= $methodCall . "\n\t\t"; |
|
| 430 | ||
| 431 | $removeAllMethod = File::get($this->stubsDirectory . '/Controller/ControllerRemoveAll.php'); |
|
| 432 | $removeAllMethod = str_replace('{{related_table_pl_uc}}', $relatedTablePluralizedUc, $removeAllMethod); |
|
| 433 | $removeAllMethod = str_replace('{{foreign_key}}', $relationship->foreignKey, $removeAllMethod); |
|
| 434 | $removeAllMethod = str_replace('{{foreign_table}}', $relationship->modelName, $removeAllMethod); |
|
| 435 | $removeAllMethod = str_replace('{{foreign_table_lw_pl}}', CamelCase::pluralize(strtolower($relationship->modelName)), $removeAllMethod); |
|
| 436 | ||
| 437 | $removeAll .= $removeAllMethod; |
|
| 438 | ||
| 439 | $removeAllCallMethod = '$this->deleteAll{{related_table_pl_uc}}(${{class_name_lw}}[\'id\']);'; |
|
| 440 | $removeAllCallMethod = str_replace('{{related_table_pl_uc}}', $relatedTablePluralizedUc, $removeAllCallMethod); |
|
| 441 | $removeAllCallMethod = str_replace('{{class_name_lw}}', $this->modelData->tableName, $removeAllCallMethod); |
|
| 442 | ||
| 443 | $removeAllCall .= $removeAllCallMethod . "\n\t\t"; |
|
| 444 | ||
| 445 | $joinRelationshipTableStub = File::get($this->stubsDirectory . 'SearchConditions/joinRelationshipTable.php'); |
|
| 446 | $joinRelationshipTableStub = str_replace('{{class_name_lw}}', $this->modelData->tableName, $joinRelationshipTableStub); |
|
| 447 | $joinRelationshipTableStub = str_replace('{{related_table_pl}}', $relatedTablePluralized, $joinRelationshipTableStub); |
|
| 448 | $joinRelationshipTableStub = str_replace('{{related_table}}', CamelCase::convertToCamelCase($relationship->relatedTable), $joinRelationshipTableStub); |
|
| 449 | $joinRelationshipTableStub = str_replace('{{foreign_key}}', $relationship->foreignKey, $joinRelationshipTableStub); |
|
| 450 | $joinRelationshipTableStub = str_replace('{{related_field}}', $relationship->relatedField, $joinRelationshipTableStub); |
|
| 451 | $joinRelationshipTableStub = str_replace('{{foreign_table}}', $relationship->tableName, $joinRelationshipTableStub); |
|
| 452 | ||
| 453 | $joins .= $joinRelationshipTableStub . "\n"; |
|
| 454 | ||
| 455 | $use = 'use App\Models\{{foreign_table}};'; |
|
| 456 | $use = str_replace('{{foreign_table}}', $relationship->modelName, $use); |
|
| 457 | ||
| 458 | $includes .= $use . "\n"; |
|
| 459 | } |
|
| 460 | } |
|
| 461 | ||
| 462 | $this->stub = str_replace('{{relationship_tables_store}}', $functions, $this->stub); |
|
| 463 | ||
| 464 | $this->stub = str_replace('{{relationship_tables_call}}', $functionsCall, $this->stub); |
|
| 465 | ||
| 466 | $this->stub = str_replace('{{remove_relationship_objects}}', $removeAll, $this->stub); |
|
| 467 | ||
| 468 | $this->stub = str_replace('{{remove_relationship_objects_call}}', $removeAllCall, $this->stub); |
|
| 469 | ||
| 470 | $this->stub = str_replace('{{relationship_tables_classes}}', $includes, $this->stub); |
|
| 471 | ||
| 472 | $this->stub = str_replace('{{relationship_tables_joins}}', $joins, $this->stub); |
|
| 473 | ||
| 474 | ||
| 475 | ||
| 476 | return $this; |
|
| 477 | ||
| 478 | } |
|
| 479 | ||
| 480 | ||
| 481 | /** |
|