Buy Now
Sign in

Saving repeater fields as json array in a single table field

bensonley , October 02 2017
B
bensonley
Im trying to work out how to save repeater fields as a json array inside a single db field. something like:
[{"title":"Work","long":-0.125243,"lat":51.511989,"type":"WORK"},{"title":"Fitness First Gym","lat":51.513201,"long":-0.138884,"type":"AMENITY"}


  • Hi,

    I get sql errors when trying to save , i think i don't fully understand the data provider / model syntax as I've tried a few variations and looked around the forum for some answers, but can't quite work it out.

    My current debug looks like this:
    Array
    (
    [option] => com_chronoforms6
    [chronoform] => add_client_schedule
    [event] => submit
    [name] => xxxx
    [email] => x@gmail.com
    [agent] => xxxx
    [agent_email] =>
    [pois] => Array
    (
    [0] => Array
    (
    [title] => gggdff
    [type] => place
    [lat] => 666
    [long] => 55
    )

    [1] => Array
    (
    [title] => gfgdsf
    [type] => amenity
    [lat] => 456456
    [long] => 66
    )

    )

    [undefined] =>
    [send] =>
    [password] => 147D914E
    [b9ef1604eaada687139b7d361fdfc189] => 3l5f8j0ng5rr2jo3e2vj60nbk3
    [a2e59797cfd7e007061e0cdfe2dddcbc] => ircbo9ehkp3sutkjs236lersp2
    [_ga] => GA1.2.92911649.1490033135
    [_gid] => GA1.2.2085845415.1506941199
    [Itemid] =>
    )

    Array
    (
    [validate_fields2] => Array
    (
    [log] => Automatic validation enabled.
    [var] => 1
    )

    [save_data9] => Array
    (
    [data] => Array
    (
    [created] => 2017-10-02 17:42:11
    [user_id] => 0
    [name] => xxxx
    [text72] =>
    [agent] => xxxx
    [agent_email] =>
    => Array
    (
    [option] => com_chronoforms6
    [chronoform] => add_client_schedule
    [event] => submit
    [name] => xxxx
    [email] => x@gmail.com
    [agent] => xxxx
    [agent_email] =>
    [pois] => Array
    (
    [0] => Array
    (
    [title] => gggdff
    [type] => place
    [lat] => 666
    [long] => 55
    )

    [1] => Array
    (
    [title] => gfgdsf
    [type] => amenity
    [lat] => 456456
    [long] => 66
    )

    )

    [undefined] =>
    [send] =>
    [password] => 147D914E
    [b9ef1604eaada687139b7d361fdfc189] => 3l5f8j0ng5rr2jo3e2vj60nbk3
    [a2e59797cfd7e007061e0cdfe2dddcbc] => ircbo9ehkp3sutkjs236lersp2
    [_ga] => GA1.2.92911649.1490033135
    [_gid] => GA1.2.2085845415.1506941199
    [Itemid] =>
    )

    [date] =>
    [address] =>
    [type] =>
    [description] =>
    [pr_lat] =>
    [pr_long] =>
    [file_photo] =>
    [bedrooms] =>
    [floors] =>
    [garage] =>
    [cost-N-] =>
    [lease] =>
    [ctax] =>
    [energy] =>
    [comm_time] =>
    [commute_type] =>
    [password] => 147D914E
    [email] => x@gmail.com
    )

    [_success] => Data saved successfully
    [log] => Array
    (
    [0] => INSERT INTO `jos_chronoengine_chronoforms_data_add_client_schedule` (`created`, `user_id`, `name`, `agent`, `agent_email`, `date`, `address`, `type`, `description`, `pr_lat`, `pr_long`, `file_photo`, `bedrooms`, `floors`, `garage`, `cost-N-`, `lease`, `ctax`, `energy`, `comm_time`, `commute_type`, `password`, `email`) values ('2017-10-02 17:42:11', '0', 'xxxx, 'xxxx', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '147D914E', 'x@gmail.com');
    )

    [var] => Array
    (
    [created] => 2017-10-02 17:42:11
    [user_id] => 0
    [name] => xxxx
    [agent] => xxxx
    [agent_email] =>
    [date] =>
    [address] =>
    [type] =>
    [description] =>
    [pr_lat] =>
    [pr_long] =>
    [file_photo] =>
    [bedrooms] =>
    [floors] =>
    [garage] =>
    [cost-N-] =>
    [lease] =>
    [ctax] =>
    [energy] =>
    [comm_time] =>
    [commute_type] =>
    [password] => 147D914E
    [email] => x@gmail.com
    [aid] => 15
    )

    )

    )


    I can attach the form if needed, any help appreciated.
    Thanks,

    Ben
    admin
    Hi Ben,

    In the "Insert data override" of the "save data", please try this:
    pois:{data.jsonen:pois}

    That will json encode the whole array inside the "pois" key submitted by the form and set it to be stored in the "pois" field in the table.

    Best regards,
    Max
    Max
    If your main question got answered then please mark the answer using the button!​
    ​Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!
    B
    bensonley
    Hi Max,

    Thanks for the reply , Actually I've managed to work it out... partially. I can save the array in a table field now using the data.jsonen shortcode in the save data action as you suggested above. I have a problem with a repeater within a repeater to try and get an array like:

    [{"id":1,"address":"14 Manette Street, Soho, London","lat":51.514781,"long":-0.130529,"cost":"680,000","type":"3 Bedroom Second floor flat","photo":"house1.jpg","description":"3 Bedroom flat with master bedroom en-suite, allocated parking plus guest space. Electronic controlled entrance with lift to all floors and entrance intercom system.","bedrooms":3,"floors":1,"garage":"2 allocated parking spaces","lease":"freehold","ctax":"Band 5","energy":"A","commute":{"walking":"9 mins","car":"5 mins","pt":"7 mins"}},{"id":6,"address":"18 Greek St, Soho, London","lat":51.5140183,"long":-0.1327134,"cost":"768,500","type":"4 Bedroom First floor flat","photo":"house1.jpg","description":"4 Bedroom flat with master bedroom en-suite, second en-suite and allocated parking. Electronic controlled entrance with lift to all floors and entrance intercom system.","bedrooms":4,"floors":1,"garage":"1 allocated parking spaces","lease":"freehold","ctax":"Band 5","energy":"A","commute":{"walking":"15 mins","car":"5 mins","pt":"9 mins"}}]


    I have the multiply buttons working , but when i click the parent repeater add button it adds 2 sets of repeated fields instead of 1, but only records the first set. I have moved the nestled repeater outside of the parent and this works fine, but then I'd have to rearrange all the data in a php script i presume to match the array above.. Or is this maybe a conflict with the nestled repeater with the multiply and remove buttons?

    I did think I could add a field asking for the number of iterations as a drop down option with values 1-5 ( as i know they are limited to 5) and use this as the key no. for the field sets for each repeater, if that makes sense?

    Thanks,

    Ben
    B
    bensonley
    Attached is my form if it's easier to see.
    Attachments
    admin
    Hi bensonley,

    Apologies for the late reply here!

    With the latest update you still have the same issue ?

    Best regards
    Max
    If your main question got answered then please mark the answer using the button!​
    ​Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!
    B
    bensonley
    Hi Max,

    I managed to fix it in after some thought about various views i wanted,I decided to change the data structure to include separate tables for properties and agents, but kept he commute field, which still saves as a json array. I used ChornoConnectivity for list views, very nice extension and works well with ChronoForms.

    Thanks

    Ben