vendredi 13 janvier 2017

How to save json data to mysql?

I am going to build a form customize system with Laravel 5.1 LTS. User could create forms, customize its fields and styles, release them then the other users could fill the form.

In the front-end side, i'm going to use Alpaca Forms. With this package, fields and datas are rendered with json.

Now the problem is how can i store fields' json and datas' json to mysql?(I don't really want to use mongodb or other nosql databases, cause i'm already using mysql now and i don't know how to deal with nosql.) The json is like this:

$("#form").alpaca({
    "data": {
        "name": "Diego Maradona",
        "feedback": "Very impressive.",
        "ranking": "excellent"
    },
    "schema": {
        "title":"User Feedback",
        "description":"What do you think about Alpaca?",
        "type":"object",
        "properties": {
            "name": {
                "type":"string",
                "title":"Name",
                "required":true
            },
            "feedback": {
                "type":"string",
                "title":"Feedback"
            },
            "ranking": {
                "type":"string",
                "title":"Ranking",
                "enum":['excellent','ok','so so'],
                "required":true
            }
        }
    },
    "options": {
        "form":{
            "attributes":{
                "action":"http://httpbin.org/post",
                "method":"post"
            },
            "buttons":{
                "submit":{}
            }
        },
        "helper": "Tell us what you think about Alpaca!",
        "fields": {
            "name": {
                "size": 20,
                "helper": "Please enter your name."
            },
            "feedback" : {
                "type": "textarea",
                "name": "your_feedback",
                "rows": 5,
                "cols": 40,
                "helper": "Please enter your feedback."
            },
            "ranking": {
                "type": "select",
                "helper": "Select your ranking.",
                "optionLabels": ["Awesome!",
                    "It's Ok",
                    "Hmm..."]
            }
        }
    },
    "view" : "bootstrap-edit"
});

I have come up with two solutions for save front-end json and one solution for save datas till now, but i don't think they are good enough, so i'm here asking for help to find a better one.

Save front-end json:

  1. list all the attributes of the front-end json, create a table with that and save all the value. It's not good to extend, if the package changes, i should update the table. the form field table is like:

    | id | form_id | type | name | rows | ... |

  2. resolve json to key-value array and save it to database. It's not good that if user creates a form, he will insert a lot of rows to table. the form field table is like:

    | id | form_id | key | value |

  3. save json as an attribute. I know Mysql 5.7 could support json, but i don't know if there is any other problems with this And Laravel 5.1 doesn't support json search. the form table is like:

    | id | json |

Save datas json:

  1. resolve json to key-value array and save it to database. It's not good that if user fills a form, he will insert a lot of rows to table. the data table is like:

    | id | form_id | key | value |

Thanks~



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire