Skip to content
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

Generating foreign-constraint and/or foreign-relation properties from command line #109

Open
CyberiaResurrection opened this issue Mar 9, 2019 · 5 comments

Comments

@CyberiaResurrection
Copy link
Contributor

Before you submit an issue please read this

This repository is only for reporting bugs or issues. If you need support, please use Stack Overflow using laravel-code-generator tag (https://stackoverflow.com/questions/tagged/laravel-code-generator)

Please provide us with details by completing the following requirements. Issues with not enough info are likely to be closed without resolution.

Environment:

Laravel-Code-Generator Version: 2.2.12
Laravel Version: 5.5.44

Description:

I'm not sure if this is a bug just in the documentation (since I couldn't find an example, or figure out how to do it) or somewhere in actual code, but how do I go about specifying foreign-relation and/or foreign-constraint values from the command line?

Using the relations parameter works as it should, generating a belongsTo relation:

php artisan resource-file:append fooOwnedModel --relations="name:fooModel;type:belongsTo;field:fooModel;params:App\FooModel|fooModel|id"

Per the docs, "When creating hasOne() or belongsTo() relations, it be best to define them at the field level using the foreign-relation option."

How do I add the above relation onto the fooModel field on fooOwnedModel when generating it at the command line. The following doesn't generate the relation I'm expecting:
php artisan resource-file:append fooOwnedModel --fields="name:fooModel;is_nullable:true;data_type:varchar;foreign_relation:name=fooModel:type=belongsTo:params|\\App\\FooModel|id:field|id" ?

Steps/Commands To Reproduce:

php artisan resource-file:append fooOwnedModel --fields="name:fooModel;is_nullable:true;data_type:varchar;foreign_relation:name=fooModel:type=belongsTo:params|\\App\\FooModel|id:field|id"

Content Of The Resource-File:

{
    "fields": [
        {
            "name": "fooModel",
            "labels": "Foo Model",
            "html-type": "text",
            "css-class": "",
            "options": {},
            "html-value": null,
            "validation": "string|min:1",
            "is-on-index": true,
            "is-on-show": true,
            "is-on-form": true,
            "data-type": "string",
            "data-type-params": [],
            "data-value": null,
            "is-index": false,
            "is-unique": false,
            "is-primary": false,
            "comment": null,
            "is-nullable": true,
            "is-header": false,
            "is-unsigned": false,
            "is-auto-increment": false,
            "is-inline-options": false,
            "is-date": false,
            "date-format": "",
            "cast-as": "",
            "placeholder": "Enter foo model here...",
            "delimiter": "; ",
            "range": [],
            "foreign-relation": null,
            "foreign-constraint": null,
            "on-store": null,
            "on-update": null
        }
    ],
    "relations": [],
    "indexes": [],
    "auto-manage-created-and-updated-at": true,
    "table-name": null,
    "protection": {
        "is-model-protected": false,
        "is-controller-protected": false,
        "is-form-request-protected": false,
        "is-languages-protected": false,
        "is-form-view-protected": false,
        "is-index-view-protected": false,
        "is-create-view-protected": false,
        "is-edit-view-protected": false,
        "is-show-view-protected": false
    }
}
MikeAlhayek added a commit that referenced this issue Mar 17, 2019
@MikeAlhayek
Copy link
Collaborator

Please upgrade to the latest version. In order to create a field from the command line you'll have to change the format to this

``-fields="name:fooModel;is-nullable:true;data-type:varchar;foreign-relation:assets#hasMany#App\Models\Asset|category_id|id"

Note: you'll need to separate the relation parts by # in order for the parser to properly parse it. Also, if you follow the naming convention, the relations will automatically get created for you.

MikeAlhayek added a commit that referenced this issue Mar 17, 2019
@CyberiaResurrection
Copy link
Contributor Author

So using your suggested fields argument should result in the following resource file? What about the second part of my issue, namely generating a foreign-constraint from the command line?

{
    "fields": [
        {
            "name": "fooModel",
            "labels": "Foo Model",
            "html-type": "select",
            "css-class": "",
            "options": {},
            "html-value": null,
            "validation": "nullable|string|min:0",
            "is-on-index": true,
            "is-on-show": true,
            "is-on-form": true,
            "data-type": "string",
            "data-type-params": [],
            "data-value": null,
            "is-index": false,
            "is-unique": false,
            "is-primary": false,
            "comment": null,
            "is-nullable": true,
            "is-header": false,
            "is-unsigned": false,
            "is-auto-increment": false,
            "is-inline-options": false,
            "is-date": false,
            "date-format": "",
            "cast-as": "",
            "placeholder": "Enter foo model here...",
            "delimiter": "; ",
            "range": [],
            "foreign-relation": {
                "name": "assets",
                "type": "hasMany",
                "params": [
                    "App\\Models\\Asset",
                    "category_id",
                    "id"
                ],
                "field": "id"
            },
            "foreign-constraint": null,
            "on-store": null,
            "on-update": null
        }
    ],
    "relations": [],
    "indexes": [],
    "auto-manage-created-and-updated-at": true,
    "table-name": null,
    "protection": {
        "is-model-protected": false,
        "is-controller-protected": false,
        "is-form-request-protected": false,
        "is-languages-protected": false,
        "is-form-view-protected": false,
        "is-index-view-protected": false,
        "is-create-view-protected": false,
        "is-edit-view-protected": false,
        "is-show-view-protected": false
    }
}

@MikeAlhayek
Copy link
Collaborator

I’ll check on the foreign constrain when time permits. However, it may be easier for you to add the relations by editing the json file directly.

@MikeAlhayek MikeAlhayek reopened this Mar 17, 2019
@CyberiaResurrection
Copy link
Contributor Author

Ok, thanks for that.

@anthonyaniah
Copy link

Greetings Sir. Could you give me artisan command to generate code for the following tables with foreign key relations:
table 1: addDept
fields=dept_id,deptName,deptHead
table 2: addFile
fields=file_id,fileRef,fileName (html = "text")

table 3: addDoc
doc_id,docRefNo,docSubject,fileRef(select from addFile),dept(select from addDept),uploadFile

table 4: searchcriteria
by by docRefNo or subject
Thanks in advance. Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants