CrestApps / laravel-code-generator

An efficient Laravel code generator, saving time by automating the creation of resources such as views, controllers, routes, migrations, languages, and form-requests. Highly flexible and customizable, it includes a cross-browser compatible template and client-side validation for application modernization.
https://laravel-code-generator.crestapps.com
MIT License
737 stars 157 forks source link

No "hasMany" relationship methods generated #23

Closed mohaghegh closed 7 years ago

mohaghegh commented 7 years ago

Hi Regarding issue #3 , "hasMany" part of the ralationships are not generated when generating resources from existing database. In my project there are two tables: countries and provinces. Running this command: php artisan create:resources Province --table-name hmk_provinces --table-exists --translation-for=en,fa --force generates a nice model file containing country method: public function country() { return $this->belongsTo('App\Models\Country','country_id'); } but the reverse method (e.g. provinces) is not generated in Country model file when running this command: php artisan create:resources Province --table-name hmk_provinces --table-exists --translation-for=en,fa --force

Am I going wrong way? Thanks a lot

MikeAlhayek commented 7 years ago

Can you please post your database scheme here?

Show Create Table hmk_provinces; Show Create Table hmk_countries;

mohaghegh commented 7 years ago

Here's the schema.

"hmk_provinces" :

CREATE TABLE `hmk_provinces` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(250) DEFAULT NULL,
 `country_id` int(11) DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `idx_provinces` (`country_id`),
 CONSTRAINT `fk_provinces` FOREIGN KEY (`country_id`) REFERENCES `hmk_countries` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8

"hmk_countries" :

CREATE TABLE `hmk_countries` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(250) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
MikeAlhayek commented 7 years ago

Thank you for sharing the schema with us.

Unfortunately that is a current limitation in the code generator. Currently it automatically handles the belongsTo relation between the objects.

However, this is something we are looking to implement in future release (possible in the next minor release.)

For now, you'll have to manually add the hasMany relation on the Country model.

Thank you for your feedback and please keep them coming :)

mohaghegh commented 7 years ago

Good. I'm sure it will be implemented as perfect as other parts of this handy code-generator :)

Watching for upcoming releases, with feedbacks and star ;)

MikeAlhayek commented 7 years ago

@mohaghegh good news! This feature have been implemented in a separate branch (i.e v2.2dev) It is not ready to be released yet, but feel free to try it out https://github.com/CrestApps/laravel-code-generator/tree/v2.2dev

There are couple things that are worth mention

  1. The --fields-file option in all command have been changed to --resource-file since that file is no longer just a fields-file. But no worries, the system is still able to parse the old fields-file format into the new resource-file format.
  2. This branch completely drop support for fields from raw string. We relay heavy on the JSON-based resource-file. The resource-file allows you to define any relations, indexes and fields all at one.
  3. The following command have been renamed
    • The command create:fields-file has been renamed to resource-file:from-database
    • The command fields-file:create has been renamed to resource-file:create
    • The command fields-file:delete has been renamed to resource-file:delete
    • The command fields-file:append has been renamed to resource-file:append
    • The command fields-file:reduce has been renamed to resource-file:reduce

Please let me know if you use it and if you encounter any unexpected bugs!

Thank you

MikeAlhayek commented 7 years ago

I am closing this ticket for now since this feature will be part of the new coming up release version 2.2.0

For more info about the new release, http://crestapps.dev/laravel-code-generator/docs/2.2