Undefined Offset: 0 When Adding New Record but update and delete work fine
Undefined Offset: 0 When Adding New Record but update and delete work fine
michaelosolinski
Posts: 12Questions: 0Answers: 0
Hi Allan,
Next problem,
I have created a new datatable where I can view results, edit and delete fine but am getting an error back when trying to create a new entry I get the following response (Please note that even though I get the error, the record is still added to the db.)
[code]
Notice: Undefined offset: 0 in /home/veh4m/public_html/nb/be-manager/DataTables/extras/Editor/php/lib/Editor/Editor.php on line 541
{"id":"row_23","fieldErrors":[],"sError":"","aaData":[],"row":null}
[/code]
JS Code
[code]
$(document).ready(function() {
var editor = new $.fn.dataTable.Editor( {
"ajaxUrl": "table.createBranch.php",
"domTable": "#createBranch",
"fields": [
{
"label" : "Branch ID",
"name" : "branchID",
},
{
"label" : "Branch Name",
"name" : "branchName",
},
{
"label" : "Address 1",
"name" : "address1",
},
{
"label" : "Address 2",
"name" : "address2",
},
{
"label" : "Post Code",
"name" : "postCode",
},
{
"label" : "City",
"name" : "city",
},
{
"label" : "Phone",
"name" : "phone",
},
{
"label" : "Fax",
"name" : "fax",
},
{
"label" : "Email",
"name" : "email",
}
]
} );
$('#createBranch').dataTable( {
"sDom": "Tfrtip",
"sAjaxSource": "table.createBranch.php",
"aoColumns": [
{
"mData": "branchID"
},
{
"mData": "branchName"
},
{
"mData": "address1"
}
],
"oTableTools": {
"sRowSelect": "single",
"aButtons": [
{ "sExtends": "editor_create", "editor": editor },
{ "sExtends": "editor_edit", "editor": editor },
{ "sExtends": "editor_remove", "editor": editor }
]
},
"fnInitComplete": function ( settings, json ) {
// Set the allowed values for the select and radio fields based on
// what is available in the database
}
} );
} );
[/code]
PHP Code
[code]
$pkey = 'branchID';
// Build our Editor instance and process the data coming from _POST
$editor = Editor::inst( $db, 'tblBranches', $pkey )
->field(
Field::inst( 'branchID' ),
Field::inst( 'branchName' ),
Field::inst( 'address1' ),
Field::inst( 'address2' ),
Field::inst( 'postCode' ),
Field::inst( 'city' ),
Field::inst( 'phone' ),
Field::inst( 'fax' ),
Field::inst( 'email' )
);
// The "process" method will handle data get, create, edit and delete
// requests from the client
$out = $editor
->process($_POST)
->data();
// When there is no 'action' parameter we are getting data, and in this
// case we want to send extra data back to the client, with the options
// for the 'department' select list and 'access' radio boxes
if ( !isset($_POST['action']) ) {
// Get Rental Period Profile Name details
}
// Send it back to the client
echo json_encode( $out );
[/code]
Next problem,
I have created a new datatable where I can view results, edit and delete fine but am getting an error back when trying to create a new entry I get the following response (Please note that even though I get the error, the record is still added to the db.)
[code]
Notice: Undefined offset: 0 in /home/veh4m/public_html/nb/be-manager/DataTables/extras/Editor/php/lib/Editor/Editor.php on line 541
{"id":"row_23","fieldErrors":[],"sError":"","aaData":[],"row":null}
[/code]
JS Code
[code]
$(document).ready(function() {
var editor = new $.fn.dataTable.Editor( {
"ajaxUrl": "table.createBranch.php",
"domTable": "#createBranch",
"fields": [
{
"label" : "Branch ID",
"name" : "branchID",
},
{
"label" : "Branch Name",
"name" : "branchName",
},
{
"label" : "Address 1",
"name" : "address1",
},
{
"label" : "Address 2",
"name" : "address2",
},
{
"label" : "Post Code",
"name" : "postCode",
},
{
"label" : "City",
"name" : "city",
},
{
"label" : "Phone",
"name" : "phone",
},
{
"label" : "Fax",
"name" : "fax",
},
{
"label" : "Email",
"name" : "email",
}
]
} );
$('#createBranch').dataTable( {
"sDom": "Tfrtip",
"sAjaxSource": "table.createBranch.php",
"aoColumns": [
{
"mData": "branchID"
},
{
"mData": "branchName"
},
{
"mData": "address1"
}
],
"oTableTools": {
"sRowSelect": "single",
"aButtons": [
{ "sExtends": "editor_create", "editor": editor },
{ "sExtends": "editor_edit", "editor": editor },
{ "sExtends": "editor_remove", "editor": editor }
]
},
"fnInitComplete": function ( settings, json ) {
// Set the allowed values for the select and radio fields based on
// what is available in the database
}
} );
} );
[/code]
PHP Code
[code]
$pkey = 'branchID';
// Build our Editor instance and process the data coming from _POST
$editor = Editor::inst( $db, 'tblBranches', $pkey )
->field(
Field::inst( 'branchID' ),
Field::inst( 'branchName' ),
Field::inst( 'address1' ),
Field::inst( 'address2' ),
Field::inst( 'postCode' ),
Field::inst( 'city' ),
Field::inst( 'phone' ),
Field::inst( 'fax' ),
Field::inst( 'email' )
);
// The "process" method will handle data get, create, edit and delete
// requests from the client
$out = $editor
->process($_POST)
->data();
// When there is no 'action' parameter we are getting data, and in this
// case we want to send extra data back to the client, with the options
// for the 'department' select list and 'access' radio boxes
if ( !isset($_POST['action']) ) {
// Get Rental Period Profile Name details
}
// Send it back to the client
echo json_encode( $out );
[/code]
This discussion has been closed.
Replies
I have a feeling that this is actually a bug in the Editor php class in that the new data is not being committed to the database until after it is asked for... That would cause this error.
To check if that is the case, could you make a small modification to Editor.php:
1. on line 244 you will find: `$this->_db->commit();` . Remove it from there.
2. Add the commit line removed immediately after line 336 (i.e. the call to `_insert()` .
3. Add the commit line also into line 581 (just before the `_get()` call).
Does that fix it?
Allan
If so I can only find $this->_db->commit(); on line 334
is this how is should look after editing?
[code]
if ( count($this->_out['fieldErrors']) === 0 ) {
if ( $data['action'] == "create" ) {
$this->_out['row'] = $this->_insert();
$this->_db->commit();
}
else {
$this->_out['row'] = $this->_update( $data['id'] );
}
}
}
}
catch (\Exception $e) {
// Error feedback
$this->_out['sError'] = $e->getMessage();
$this->_db->rollback();
}
return $this;
}
[/code]
I can't paste the full code from the editor.php file so you can see it here http://pastebin.com/JmmaVrwh . I don't seem to have a _get() call on line 581
One other thing I have noticed is that if I try and edit the field that is the primary key for the record, I also get the same error but it updates in the database and within the datatable when I click refresh. I am assuming this is the same issue though?
Completely the wrong number - sorry about that. yes your edit looks fine.
> I don't seem to have a _get() call on line 581
No - the _get call is on like 584 in your paste. I was suggesting that you put the commit call on like 581, i.e. before the _get call.
> One other thing I have noticed is that if I try and edit the field that is the primary key for the record, I also get the same error but it updates in the database and within the datatable when I click refresh. I am assuming this is the same issue though?
The current code isn't set up to work with a change to the primary key on edit - I had assumed that that wouldn't happen - the primary key always stays the same. That's obviously not the case here - but, daft question perhaps, why would you update the primary key - wouldn't that break referential integrity in the db if you have any joins to the current table?
The code could be updated to cope with this, and perhaps should be, but I'm not sure what the use case for changing the primary key is?
Allan
The following is the response when trying to add a new record:
[code]
Notice: Undefined offset: 0 in /home/veh4m/public_html/nb/be-manager/DataTables/extras/Editor/php/lib/Editor/Editor.php on line 542
{"id":"row_27","fieldErrors":[],"sError":"","aaData":[],"row":null}
[/code]
After making the changes, I am also no longer able to delete records. When I click delete then the record disappears from the datatable but is not removed from the database and if I refresh the page is shows again in the datatable. Editing the record still works fine.
I don't have this issue with the previous editor that we were working on so is it something specific to this database table?
[code]
if ( count($this->_out['fieldErrors']) === 0 ) {
if ( $data['action'] == "create" ) {
$this->_out['row'] = $this->_insert();
$this->_db->commit();
}
else {
$this->_out['row'] = $this->_update( $data['id'] );
}
}
}
}
catch (\Exception $e) {
// Error feedback
$this->_out['sError'] = $e->getMessage();
$this->_db->rollback();
}
[/code]
and for the added commit call
[code]
// And the join tables
for ( $i=0 ; $i_join) ; $i++ ) {
$this->_join[$i]->update( $this->_db, $id, $this->_formData );
}
$this->_out['id'] = $this->_idPrefix . $id;
// Get the data for the row so we can feed it back to the client and redraw
// the whole row with the full data set from the server.
$this->_db->commit();
$row = $this->_get( $id );
return $row['aaData'][0];
[/code]
If its the primary key issue, it sounds like an error specific to this version of the Editor files, but if it was working before, it wasn't by design! I had never expected the primary key to be updated on edit.
If it is the primary key that is changing, you could try adding this just before line 6 in the last code block above (near the end of the _update() function):
[code]
$id = $this->_formData[ $this->pkey() ];
[/code]
assuming that the primary key name submitted to the server is the same as what is in the database?
Allan
The issue with editing the primary key I can understand and live with. To be fair it is probably more a case of me using the wrong value as the primary key in this instance as although it is a unique value for each record, there is the potential for it to change in the future.
To simplify matters and in the interests of best practice moving forward, I have changed the primary key and having done so and reverting back to the original editor.php code everything seems to be working correctly including the ability to edit the unique branch id that I was having issues with.
Excellent to hear that it is working now :-).
I'm working on Editor 1.3 just now, so I will look at what I can do about improving this area. Thanks for the feedback on it!
Regards,
Allan