Concurrent Update and insert in grid don't work as expected


If I add a new row and edit another row then when pressing Save the connector correctly updates the edited rows but fails to insert the NEW ROW properly when it triggers the “beforeInsert” event.

If I only add a new row then it works fine.

It only gets confused when updates and inserts are sent at the same time.

$gridConn->event->attach("beforeInsert","comid_insert");

function comid_insert($data){
    global $comid; //get comid value from isloggedin

    $data->add_field("comid",$comid); //will be included in insert

 }

Enabled logging and got this message:

Row data [1367423299596]
id => 
make => NEW ROW
model => NEW ROW
license_plate => NEW ROW
leased => 0
contract_expiration => 2013-05-15
monthly_cost => 1234
!nativeeditor_status => inserted


adding field: comid, with value: 1

INSERT INTO ben_cars(`id`,`make`,`model`,`license_plate`,`leased`,`contract_expiration`,`monthly_cost`) VALUES ('','NEW ROW','NEW ROW','NEW ROW','0','2013-05-15','1234')

As you notice the field “comid” is not added in the SQL command.

Sorry for bumping topic but I wonder if there is any solution for this

a) you have id as editable field in grid, which may cause issues for GridConnector ( if you are using KeyGridConnector - its fine )

The code looks fine in other aspects, so I’m not sure why extra field is not added to the insert command. Can you provide full code of connector’s initialization ?

Thanks for the reply.

Let me remind you that if I only insert a row then “add_field” works fine, bug appears when I insert AND edit rows at the same time.

a) ID is not editable , it’s defined as RO in grid

require("../codebase/connector/grid_connector.php");
require("../codebase/connector/db_mysqli.php");

$mysqli = new mysqli('localhost', 'user', 'pass', 'db'); 
mysqli_set_charset($mysqli, 'utf8');
$gridConn = new GridConnector($mysqli,"MySQLi");

 $gridConn->event->attach("beforeInsert","comid_insert");  //Make sure comid is added in insert sql statement

function comid_insert($data){
    global $comid; //get comid value from isloggedin

    $data->add_field("comid",$comid); //will be included in insert

 }

$gridConn->render_sql("select id,make,model,license_plate,leased,contract_expiration,monthly_cost from ben_cars where comid = $comid","id","id,make,model,license_plate,leased,contract_expiration,monthly_cost");

Any ideas why this happens?

a) ID is not editable , it’s defined as RO in grid
Still it may interfere, during new row creating, connector will receive two id values ( id of new row, and value of related column in grid ) which can break correct after-insert procession.

Usage of KeyGridConnector may help in above case.

Please try to enable server side logging
docs.dhtmlx.com/doku.php?id=dhtm … tor:errors
and provide results for the problematic operation.

Same example:


Here is the logged info:

====================================
Log started, 13/05/2013 03:27:07
====================================

2013-05-13 15:27:07 	ma_ORGANIZATION_grid.php : ma_CARS
2013-05-13 15:27:07 	
DataProcessor object initialized
9_gr_id => 9
9_c0 => 9
9_c1 => EDITED ROW
9_c2 => EDITED ROW val2
9_c3 => 11
9_c4 => 0
9_c5 => 2013-05-07
9_c6 => 123.00
9_!nativeeditor_status => updated
1368447966416_gr_id => 1368447966416
1368447966416_c0 => 
1368447966416_c1 => NEW ROW val1
1368447966416_c2 => NEW ROW val2
1368447966416_c3 => newrow val3
1368447966416_c4 => 0
1368447966416_c5 => 2013-05-08
1368447966416_c6 => 111
1368447966416_!nativeeditor_status => inserted
ids => 9,1368447966416

Row data [9]
id => 9
make => EDITED ROW
model => EDITED ROW val2
license_plate => 11
leased => 0
contract_expiration => 2013-05-07
monthly_cost => 123.00
!nativeeditor_status => updated

UPDATE ben_cars SET `id`='9',`make`='EDITED ROW',`model`='EDITED ROW val2',`license_plate`='11',`leased`='0',`contract_expiration`='2013-05-07',`monthly_cost`='123.00' WHERE `id`='9' AND (( comid = 1))

Row data [1368447966416]
id => 
make => NEW ROW val1
model => NEW ROW val2
license_plate => newrow val3
leased => 0
contract_expiration => 2013-05-08
monthly_cost => 111
!nativeeditor_status => inserted

adding field: comid, with value: 1

INSERT INTO ben_cars(`id`,`make`,`model`,`license_plate`,`leased`,`contract_expiration`,`monthly_cost`) VALUES ('','NEW ROW val1','NEW ROW val2','newrow val3','0','2013-05-08','111')

Edit operation finished
0 => action:updated; sid:9; tid:9;
1 => action:inserted; sid:1368447966416; tid:15;

Done in 0.068323135375977s

Please try to update connector’s files with attached ones.
codebase_connector_.zip (68.1 KB)

Unfortunately it’s still not working :frowning:

Also tried with keygrid_connector, but I get same behavior

Please check the attached sample.
con_add_sample.zip (670 KB)

Thank you very much for the sample, tried to add remove_field and use keygrid but I got the same behavior.

One major difference with your sample is that it automatically saves with each change made in grid. In my case the user presses “Save” button to save the changes .

Another difference is that I use render_sql instead of render_table

DataProcessor object initialized
2_gr_id => 2
2_c0 => 2
2_c1 => IT
2_c2 => Information Technology Dept
2_!nativeeditor_status => updated
1369223381862_gr_id => 1369223381862
1369223381862_c0 => 
1369223381862_c1 => www
1369223381862_c2 => wwww
1369223381862_!nativeeditor_status => inserted
ids => 2,1369223381862

Row data [2]
id => 2
code => IT
description => Information Technology Dept
!nativeeditor_status => updated

UPDATE departments SET `id`='2',`code`='IT',`description`='Information Technology Dept' WHERE `id`='2' AND (( comid = 1))

Row data [1369223381862]
id => 
code => www
description => wwww
!nativeeditor_status => inserted

adding field: comid, with value: 1

removing field: id

INSERT INTO departments(`id`,`code`,`description`) VALUES ('','www','wwww')

I modified your example like this and it fails to insert the new row:

<body>
	<div id='grid_here' style='width:600px; height:300px'></div>
	<script type="text/javascript">
		mygrid = new dhtmlXGridObject('grid_here');
		mygrid.setImagePath("./dhtmlx/imgs/");
		mygrid.setColTypes("ro,ed,ed");
		mygrid.setHeader("id, make, model");
		mygrid.setInitWidths("50,150,*")
		mygrid.setSkin("dhx_skyblue")
		mygrid.init();
		mygrid.loadXML("./data.php");

		var dp = new dataProcessor("./data.php");
		dp.enablePartialDataSend(false);
        dp.setUpdateMode("off");
		dp.init(mygrid)

		function add_row(){
			var id = mygrid.uid();
			mygrid.addRow(id, "");
			setTimeout(function(){
				mygrid.selectCell(mygrid.getRowIndex(id),1);
				mygrid.editCell();
			},100);
		}
		
		function save_grid()
		{
			dp.sendData("");
		}
	</script>
	<input type='button' value='add' onclick='add_row()'>  
	<input type='button' value='save' onclick='save_grid()'>  
</body>

And here is the log I got:



====================================
Log started, 22/05/2013 03:03:30
====================================

SELECT id,id,make,model FROM ben_cars

Done in 0.060813903808594s



====================================
Log started, 22/05/2013 03:03:41
====================================

DataProcessor object initialized
4_gr_id => 4
4_c0 => 4
4_c1 => adasdas
4_c2 => 4444
4_!nativeeditor_status => updated
1369224194225_gr_id => 1369224194225
1369224194225_c0 => 
1369224194225_c1 => 55555
1369224194225_c2 => 555555
1369224194225_!nativeeditor_status => inserted
ids => 4,1369224194225

Row data [4]
id => 4
make => adasdas
model => 4444
!nativeeditor_status => updated

UPDATE ben_cars SET id='4',make='adasdas',model='4444' WHERE id='4'

Row data [1369224194225]
id => 
make => 55555
model => 555555
!nativeeditor_status => inserted

adding field: comid, with value: 1

removing field: id

INSERT INTO ben_cars(id,make,model) VALUES ('','55555','555555')

SQLite3::query(): Unable to execute statement: datatype mismatch at /var/www/sample1/connector/db_sqlite3.php line 14

exception 'Exception' with message 'SQLLite - sql execution failed
datatype mismatch' in /var/www/sample1/connector/db_sqlite3.php:16
Stack trace:
#0 /var/www/sample1/connector/db_common.php(647): SQLite3DBDataWrapper->query('INSERT INTO ben...')
#1 [internal function]: DBDataWrapper->insert(Object(DataAction), Object(DataRequestConfig))
#2 /var/www/sample1/connector/dataprocessor.php(217): call_user_func(Array, Object(DataAction), Object(DataRequestConfig))
#3 /var/www/sample1/connector/dataprocessor.php(168): DataProcessor->check_exts(Object(DataAction), 'insert')
#4 /var/www/sample1/connector/dataprocessor.php(97): DataProcessor->inner_process(Object(DataAction))
#5 /var/www/sample1/connector/base_connector.php(490): DataProcessor->process(Object(DataConfig), Object(DataRequestConfig))
#6 /var/www/sample1/connector/base_connector.php(409): Connector->render()
#7 /var/www/sample1/data.php(16): Connector->render_table('ben_cars', 'id', 'id, make,model')
#8 {main}

Edit operation finished
0 => action:updated; sid:4; tid:4;
1 => action:error; sid:1369224194225; tid:1369224194225;

Done in 0.12990212440491s

I was able to repeat the problem, it occurs only in case of delayed saving, when rows not only inserted but updated as well.
Anyway - fixed php files is attached, try to replace original one in connector folder with it
dataprocessor.zip (3.6 KB)

Thanks a lot, will try that.

Problem solved, thanks a lot.