Editor Select2 Multiple, Error: Array to string conversion in Query.php

Editor Select2 Multiple, Error: Array to string conversion in Query.php

vincmeistervincmeister Posts: 136Questions: 36Answers: 4
edited June 2016 in Editor

Hello Allan,

I want to allow multiple on editor's field by using this example

There is an submit error on nik field. this is the multiple value's field

<b>Notice</b>:  Array to string conversion in <b>\php\Database\Driver\Mysql\Query.php</b> on line <b>93</b><br />
{"data":[{"DT_RowId":"row_305","emp_activity":{"nik":"Array","effective_date":"11 Jul 2016","leaves_category":"5","reason":"IDUL FITRI","submitted_by":"3","submitted_date":"2016-06-10 15:40:49"},"master_employee":{"full_name":null},"emp_activity_cat":{"emp_activity_category":"CUTI"}}]}

Debug
Please advise, thank you

danny

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    If you use multiple for Select2 then it will submit an array. It sounds like your server-side script is expecting a string. You could use the implode formatter to convert from array to string. Also make sure you use explode for the get formatter.

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    thanks for the explanation Allan. Is there any example?
    I'm trying to read this doc, but still confuse how to put the implode

    danny

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    There are examples of how to use the formatters on the formatter documentation page I linked to. For example you might use something like:

    Field::inst( 'myField' )
        ->getFormatter( 'Format::explode' )
        ->setFormatter( 'Format::implode' )
    

    Assuming you want to store it in a single text field in your database of course.

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    hello Allan,

    thank you for the example. Unfortunately, only the last value of my ```select2`` that stored to database.

    Let's say i have 4 fields, 1 of them using select2
    when user input 3 values on 1st field, what i want is the editor submit 3 rows to mysql

    what i got from this code below is only last value of the 1st field that submitted to mysql. please help again, thank you

    here's my php file
    ```php
    <?php
    // DataTables PHP library
    include( "../php/DataTables.php" );

    // Alias Editor classes so they are easy to use
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate;
    
    Editor::inst( $db, 'emp_activity' )
        ->field(
            Field::inst( 'emp_activity.nik' )
                ->validator( 'Validate::notEmpty')
                ->options( 'master_employee', 'nik', 'full_name', function ($q) {
                        $q->where( 'resign_date', '0000-00-00', '=' );
                    })
                //->getFormatter( 'Format::explode' )
                ->setFormatter( 'Format::implode' ),
            Field::inst( 'master_employee.full_name' ),
            Field::inst( 'emp_activity.effective_date' )
                ->validator( 'Validate::dateFormat', array(
                    'empty' => false,
                    'format' => 'd M Y'
                ) )
                ->getFormatter( 'Format::datetime', array(
                    'from' => 'Y-m-d',
                    'to' =>   'd M Y'
                ) )
                ->setFormatter( 'Format::datetime', array(
                    'from' => 'd M Y',
                    'to' =>   'Y-m-d'
                ) ),
            Field::inst( 'emp_activity.leaves_category' )
                ->validator( 'Validate::notEmpty')
                ->options( 'emp_activity_cat', 'id', 'emp_activity_category' ),
            Field::inst( 'emp_activity_cat.emp_activity_category' ),
            Field::inst( 'emp_activity.reason' )
                ->validator( 'Validate::notEmpty' ),
            Field::inst( 'emp_activity.submitted_by' ),
            Field::inst( 'emp_activity.submitted_date' )
            )
        ->leftJoin( 'emp_activity_cat', 'emp_activity_cat.id', '=', 'emp_activity.leaves_category' )
        ->leftJoin( 'master_employee', 'master_employee.nik', '=', 'emp_activity.nik' )
        ->leftJoin( 'master_department', 'master_department.Dept_ID', '=', 'master_employee.sub_department' )
        ->process($_POST)
        ->json();
    
    <?php > ``` ?>
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    when user input 3 values on 1st field, what i want is the editor submit 3 rows to mysql

    Sorry - that is not something that Editor does. It will submit a single row only for each row that was edited or created. If you need to break the submitted data out into three rows you would need to use some custom code on the server-side - the pre-built PHP libraries will not provide that functionality.

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4
    edited June 2016

    OK Allan, thank you for the explanation
    Feature requests maybe :)

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    To be honest it is unlikely the pre-built libraries will ever support such an action, but it is something I have added to the "blue sky" list.

    Allan

This discussion has been closed.