Notice: Array to string conversion

Notice: Array to string conversion

KristapsKristaps Posts: 4Questions: 0Answers: 0
edited April 2014 in Editor
Hi!

Here is situation.

I have 2 tables:

IP (IP_ID, IP_TYPE, ...)
IP_TYPE (CODE, IP_NAME)

Editor server-side script with join on IP.IP_TYPE = IP_TYPE.CODE :
[code]
$editor = Editor::inst( $db, 'INV_P.IP', 'IP_ID' )
->field(
Field::inst( 'IP_ID' ),
Field::inst( 'IP_TYPE' ),
)
->join(
Join::inst( 'IP_TYPE', 'object' )
->join('IP_TYPE','CODE')
->field(
Field::inst( 'CODE' ),
Field::inst( 'IP_NAME' )
)
);
[/code]

Client side JS:
[code]
editor = new $.fn.dataTable.Editor( {
"fields": [
{
"label": "IP_ID:",
"name": "IP_ID"
}, {
"label": "Test IP type:",
"name": "IP_TYPE.CODE",
"type": "select"
}
]
});
myTable=$("#dt_data").dataTable( {
"aoColumns": [
{ "mData": "IP_ID" },
{ "mData": "IP_TYPE.IP_NAME" }
],

"fnInitComplete": function ( settings, json ) {
// Set the allowed values for the select and radio fields based on
// what is available in the database
editor.field("IP_TYPE.CODE").update( json.select_ip_type );
}
} );
[/code]

P.S. I removed all not relevant columns in this example.

1. Table rows show joined column names correctly.
2. When I open "Edit window", I see dropdown values (for joined table) good with correct default value selected.

3. PROBLEM: When I save changes, I get this error "Notice: Array to string conversion in ...".

Client sends following data:
[code]
action:edit
table:
id:row_15065
data[IP_ID]:15065
data[IP_TYPE][CODE]:2
[/code]

Problem happens here:
[code]
// bind values
for ( $i=0 ; $i_value) ; $i++ ) {
//echo 'Binding: {:'.$this->_field[$i] .'} as {'. $this->_value[$i]."}\n";
$this->_stmt->bindValue( ':'.$this->_field[$i], $this->_value[$i] );
}
[/code]

var_dump($this->_value) which causes problem (array in array!):
[code]
array(20) {
[0]=>
string(5) "15065"
[1]=>
array(1) {
["CODE"]=>
string(1) "2"
}
}
[/code]

Any ideas what I'm doing wrong?

Replies

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

    Thanks for the great post!

    I suspect that you want to change:

    > "name": "IP_TYPE.CODE",

    to:

    [code]
    "name": "IP_TYPE",
    [/code]

    Reason I suggest that is that the above change will change the `IP_TYPE` column on the `INV_P.IP` table. As you have it at the moment, Editor is submitting information to edit the join, and not the `IP_TYPE` column on the parent table.

    You can also add `->set( false )` onto your Join instance to make sure Editor doesn't mess around with the joined table.

    Allan
  • KristapsKristaps Posts: 4Questions: 0Answers: 0
    I've tried that already.
    When I change "name:IP_TYPE" then dropdown field in editor is empty. So I assume that I have to change "fnInitComplete" line, too:
    [quote]
    editor.field("IP_TYPE.CODE").update( json.select_ip_type );
    [/quote]

    to:
    [quote]
    editor.field("IP_TYPE").update( json.select_ip_type );
    [/quote]

    In this case I loose default (selected) value in dropdown.
    When I press save changes then I get correct UPDATE statement but... with following (undesired) INSERT INTO IP_TYPE table

    And yes, I'm using "->set( false )" with Join instance as you suggested. I've tried it before but it didn't change anything in this case.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Could you try using the following and let me know how you get on with it please? Also, can you confirm you are using Editor 1.2.4 and the PHP libraries that came with it?

    JS:

    [code]
    editor = new $.fn.dataTable.Editor( {
    "fields": [
    {
    "label": "IP_ID:",
    "name": "IP_ID"
    }, {
    "label": "Test IP type:",
    "name": "IP_TYPE",
    "type": "select"
    }
    ]
    });
    myTable=$("#dt_data").dataTable( {
    "aoColumns": [
    { "mData": "IP_ID" },
    { "mData": "IP_TYPE.IP_NAME" }
    ],

    "fnInitComplete": function ( settings, json ) {
    // Set the allowed values for the select and radio fields based on
    // what is available in the database
    editor.field("IP_TYPE").update( json.select_ip_type );
    }
    } );
    [/code]

    PHP:

    [code]
    $editor = Editor::inst( $db, 'INV_P.IP', 'IP_ID' )
    ->field(
    Field::inst( 'IP_ID' ),
    Field::inst( 'IP_TYPE' ),
    )
    ->join(
    Join::inst( 'IP_TYPE', 'object' )
    ->join('IP_TYPE','CODE')
    ->set( false )
    ->field(
    Field::inst( 'CODE' ),
    Field::inst( 'IP_NAME' )
    )
    );
    [/code]

    If the value isn't selected automatically on edit, can you confirm what `json.select_ip_type` contains please?

    Are you are able to link me to the page, that might be quite helpful so I can take a look at what is happening first hand.

    Allan
  • KristapsKristaps Posts: 4Questions: 0Answers: 0
    edited April 2014
    Yes, I'm using 1.2.4 version. I created Oracle driver but i tried not to change anything related to Editor/Datatables code. I only extended Query and Result classes. And I can't give you access because it is on intranet site behind firewall.

    Regarding my problem...
    I used javascript debugger and here is what I found:
    *) when I use long notation i.e. "name: IP_TYPE.CODE" then I get correct dropdown selected value. Check this screenshot (value=9 is correct selectedIndex):
    https://drive.google.com/file/d/0B96P5K8xER4wQ1dBcmF3LWRwZTA
    Problem: in this case I get error when I try to save data.

    *) when I use short notation i.e. "name: IP_TYPE" then I get object returned:
    https://drive.google.com/file/d/0B96P5K8xER4wRU9IbnI4S0FmcDg
    and in this case selectbox property "selectedIndex=-1"
    Problem: In this case I can save data fine but I loose select box default value each time I open edit window (popup).
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    The short notation is what you want to use (assuming you want to modify the `INV_P.IP` table?). The long notation will not work for that, so we need to resolve the error that is occurring.

    I think I see what the issue is - basic you have a join table with the same name as a field the join is overwriting the individual value. The `aliasParentTable ` method can be used to workaround this in 1.2: https://editor.datatables.net/docs/current/php/class-DataTables.Editor.Join.html#_aliasParentTable

    There is an example of it in action here: https://editor.datatables.net/release/DataTables/extras/Editor/examples/joinSelf.html .

    PHP:

    [code]
    $editor = Editor::inst( $db, 'INV_P.IP', 'IP_ID' )
    ->field(
    Field::inst( 'IP_ID' ),
    Field::inst( 'IP_TYPE' ),
    )
    ->join(
    Join::inst( 'IP_TYPE', 'object' )
    ->aliasParentTable( 'INNER_TYPE' )
    ->name( 'INNER_TYPE' )
    ->join('IP_TYPE','CODE')
    ->set( false )
    ->field(
    Field::inst( 'CODE' ),
    Field::inst( 'IP_NAME' )
    )
    );
    [/code]

    JS:

    [code]
    editor = new $.fn.dataTable.Editor( {
    "fields": [
    {
    "label": "IP_ID:",
    "name": "IP_ID"
    }, {
    "label": "Test IP type:",
    "name": "IP_TYPE",
    "type": "select"
    }
    ]
    });
    myTable=$("#dt_data").dataTable( {
    "aoColumns": [
    { "mData": "IP_ID" },
    { "mData": "INNER_TYPE.IP_NAME" }
    ],

    "fnInitComplete": function ( settings, json ) {
    // Set the allowed values for the select and radio fields based on
    // what is available in the database
    editor.field("IP_TYPE").update( json.select_ip_type );
    }
    } );
    [/code]

    I can't test it of course, but I think that should work...

    The reason I highlight that this is a workaround for 1.2 is that the join functionality is hugely improved in the upcoming 1.3. If we can't get the above working for you, I'll send over a beta copy of 1.3 with its new `leftJoin()` method which is much more SQL like!

    Allan
  • KristapsKristaps Posts: 4Questions: 0Answers: 0
    edited April 2014
    Working perfect!

    I had feeling that problem might be with same table/column names but I didn't know how to create alias in datatables editor. And unfortunately I coudn't change table naming or structure because that's 10 years old Oracle database where I need to create simple frontend (without structural changes).

    Thanks Allan.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Excellent! That's good to hear. As I say, the new join work in 1.3 will hopefully make this all a bit easier :-).

    Allan
This discussion has been closed.