Allart Softworks
Allart Softworks

Hi, I have a customer table and model using some repeater fields for phones, mails and addresses.

I use Import Export behavior.

I get "Array to string conversion" error if I don't use json_encode to export data:

namespace MyCompany\MyPlugin\Models;

use Model;
use Backend\Models\ExportModel;
use MyCompany\MyPlugin\Models\Customer;

class ExportCustomer extends \Backend\Models\ExportModel {

public function exportData($columns, $sessionKey = null) {
    $customers = Customer::all();

    $customers->each(function($customer) use ($columns) {
        foreach ($columns as $column) {
            if (is_array($customer->$column)) {
                $customer->$column = json_encode($customer->$column);
            }
            $customer->addVisible($column);
        }
    });

    return $customers->toArray();
    }

}

But this way the resulting .csv is useless to my customer needs (he needs to manually edit it before uploading it in order to populate the backend).

The goal is to have a prefixed column for each repeater field, so that my customer can edit easily, this way ("contact" is the repeater field):

| id | business_name | contact_1_name | contact_1_email | contact_1_mobile | contact_2_name | contact_2_email | contact_2_mobile | contact_3_name | contact_3_email | contact_3_mobile | ...

The data in my "contacts" column are json_encoded, indeed:

[{"name":"Eric Lucas","surname":"Riddle","role":"Manager","phone":"+1 (989) 305-8603","cellphone":"+1 (298) 758-1686","email":"calyqev@mailinator.com","linkedin":"mylinkedin"}]

Nonetheless, if I edit it and try to import it via the ImportCustomers model, I fill all the json_encoded tables with escaped data and I can't retrieve it on my view. The other fields are displayed correctly.

class ImportCustomer extends \Backend\Models\ImportModel
{
    public $rules = [];

    public function importData($results, $sessionKey = null)    {

        foreach ($results as $row => $data) {

            try {
                $customer = new Customer;
                $customer->fill($data);
                $customer->save();

                $this->logCreated();
            }
            catch (\Exception $ex) {
                $this->logError($row, $ex->getMessage());
            }

        }
    }
}

Any ideas on how to manage repeater fields?

1-1 of 1