One table - One Column - One Primary Key
One table - One Column - One Primary Key
Hi,
I am using the Editor (1.2.3) with the PHP library for users to modify a table (admin_user) which has only one column (username) and that column is the primary key.
On update, I get a "Undefined offset: 0 in ...datatable/php/lib/Editor/Editor.php on line 544". Which is fair, it's a primary key, you wouldn't want to be able to do that usually... Also I want to here :D And the field actually get modified in the DB but the user sees the "an error has occured ..." message.
On delete, everything goes fine.
On create, I get a "offset: 0 in ...datatable/php/lib/Editor/Editor.php on line 507" (The row gets added in the DB and I get the "an error has occured ..." as well here). And it's my understanding that this should not happen and I have the feeling I am doing something wrong: I just can't figure out what, so here is my code!!
The JS and HTML:
[code]
var editor;
$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
"ajaxUrl": "assets/datatable/php/ajax_adminusers.php",
"domTable": "#example",
"fields": [ {
"label": "User Name (NT ID):",
"name": "username"
}
]
} );
// New record
$('#btn_editor_create').click(function (e) {
e.preventDefault();
editor.create(
'Create new record',
{ "label": "Add", "fn": function () { editor.submit() } }
);
} );
// Edit record
$('#example').on('click', 'a.editor_edit', function (e) {
e.preventDefault();
editor.edit(
$(this).parents('tr')[0],
'Edit record',
{ "label": "Update", "fn": function () { editor.submit() } }
);
} );
// Delete a record (asking a user for confirmation)
$('#example').on('click', 'a.editor_remove', function (e) {
e.preventDefault();
editor.message( "Are you sure you want to remove this row?" );
editor.remove( $(this).parents('tr')[0], 'Delete row', {
"label": "Confirm",
"fn": function () { this.submit(); }
});
});
$('#example').dataTable( {
"sDom": "lfrtip",
"iDisplayLength": 50,
"sAjaxSource": "assets/datatable/php/ajax_adminusers.php",
"aoColumns": [
{ "mData": "username" },
{
"mData": null,
"sClass": "center",
"sDefaultContent": 'Edit / Delete'
}
]
});
} );
Create New Record
Username
Admin
[/code]
And the PHP:
[code]
include( "lib/DataTables.php" );
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Join,
DataTables\Editor\Validate;
Editor::inst( $db, 'admin_user', 'username' )
->fields(
Field::inst( 'username' )->validator( 'Validate::required' )
)
->process( $_POST )
->json();
[/code]
Any help/pointer/suggestion would be appreciated!
Thanks
I am using the Editor (1.2.3) with the PHP library for users to modify a table (admin_user) which has only one column (username) and that column is the primary key.
On update, I get a "Undefined offset: 0 in ...datatable/php/lib/Editor/Editor.php on line 544". Which is fair, it's a primary key, you wouldn't want to be able to do that usually... Also I want to here :D And the field actually get modified in the DB but the user sees the "an error has occured ..." message.
On delete, everything goes fine.
On create, I get a "offset: 0 in ...datatable/php/lib/Editor/Editor.php on line 507" (The row gets added in the DB and I get the "an error has occured ..." as well here). And it's my understanding that this should not happen and I have the feeling I am doing something wrong: I just can't figure out what, so here is my code!!
The JS and HTML:
[code]
var editor;
$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
"ajaxUrl": "assets/datatable/php/ajax_adminusers.php",
"domTable": "#example",
"fields": [ {
"label": "User Name (NT ID):",
"name": "username"
}
]
} );
// New record
$('#btn_editor_create').click(function (e) {
e.preventDefault();
editor.create(
'Create new record',
{ "label": "Add", "fn": function () { editor.submit() } }
);
} );
// Edit record
$('#example').on('click', 'a.editor_edit', function (e) {
e.preventDefault();
editor.edit(
$(this).parents('tr')[0],
'Edit record',
{ "label": "Update", "fn": function () { editor.submit() } }
);
} );
// Delete a record (asking a user for confirmation)
$('#example').on('click', 'a.editor_remove', function (e) {
e.preventDefault();
editor.message( "Are you sure you want to remove this row?" );
editor.remove( $(this).parents('tr')[0], 'Delete row', {
"label": "Confirm",
"fn": function () { this.submit(); }
});
});
$('#example').dataTable( {
"sDom": "lfrtip",
"iDisplayLength": 50,
"sAjaxSource": "assets/datatable/php/ajax_adminusers.php",
"aoColumns": [
{ "mData": "username" },
{
"mData": null,
"sClass": "center",
"sDefaultContent": 'Edit / Delete'
}
]
});
} );
Create New Record
Username
Admin
[/code]
And the PHP:
[code]
include( "lib/DataTables.php" );
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Join,
DataTables\Editor\Validate;
Editor::inst( $db, 'admin_user', 'username' )
->fields(
Field::inst( 'username' )->validator( 'Validate::required' )
)
->process( $_POST )
->json();
[/code]
Any help/pointer/suggestion would be appreciated!
Thanks
This discussion has been closed.
Replies
The updates I've made to Editor 1.3 will "mask" this error - in that if Editor can't get a row, rather than throwing an error, it will remove it form the table. But I rather suspect that isn't what you want in this case.
I think the Editor code should probably cope with this...
In Editor.php at the bottom of the `_update()` method you will find:
[code]
$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.
$row = $this->_get( $id );
[/code]
Replace it with:
[code]
// Was the primary key altered as part of the edit?
$getId = isset( $this->formData['data'][ $this->_pkey ] ) ?
$this->formData['data'][ $this->_pkey ] :
$id;
$this->_out['id'] = $this->_idPrefix . $getId;
// 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.
$row = $this->_get( $getId );
[/code]
If you could try that and let me know how you get on that would be great!
Allan
The method is `private function _update( $id )` - it is just before the insert function (insert for a new record, and update for an edit). You mentioned that it was on update you are having an issue.
Allan
I actually have a problem for both an insert (new record) and update (edit).
I made the change in _update and it works great. Anything I can do in insert for the error message to go away?
Allan
Allan
[code]
CREATE TABLE `admin_user` (
`username` varchar(45) NOT NULL,
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
[/code]
And ajax_adminusers.php:
[code]
include( "lib/DataTables.php" );
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Join,
DataTables\Editor\Validate;
Editor::inst( $db, 'admin_user', 'username' )
->fields(
Field::inst( 'username' )->validator( 'Validate::required' )
)
->process( $_POST )
->json();
[/code]
Allan
Allan
Based on this thread: http://stackoverflow.com/questions/11156735/last-insert-id-is-not-returning , which says:
> lastInsertId() only returns IDs automatically generated in an AUTO_INCREMENT column.
So - the fix is to use the id that you submit.
In the `_insert()` function (Editor.php again) you will find:
> $id = $res->insertId();
Immediately after that, add:
[code]
if ( isset( $this->_formData[ $this->_pkey ] ) ) {
$id = $this->_formData[ $this->_pkey ];
}
[/code]
And that should do it.
I've just tried it here and it seems to work well for create, edit and delete.
These changes will be in 1.3 :-)
Allan