Fatal Error while setting up DataTables Editor

I’m building a CRUD application using DataTables API and trying to set up Editor. After some help from the DataTables Team, I was able to get partially through the setup process, but Editor is not pulling any records from my database into DataTables. They mentioned that I needed to update the config.php file which contains the information needed to connect to the database. When I updated it, I see this:

Fatal error: Uncaught Error: Call to a member function transaction() on string in C:\xampp\htdocs\ccrp\api\lib\Editor.php:964 Stack trace: #0 C:\xampp\htdocs\ccrp\api\lib\Editor.php(700): DataTables\Editor->_process(Array) #1 C:\xampp\htdocs\ccrp\api\server_data.php(53): DataTables\Editor->process(Array) #2 {main} thrown in C:\xampp\htdocs\ccrp\api\lib\Editor.php on line 964

I am not sure what this error means or how to fix it. The DataTables team says it may have something to do with the config.php file I updated, so here is my current config file:

<?php if (!defined('DATATABLES')) exit(); // Ensure being used in DataTables env.

/*
 * DB connection script for Editor
 * Created by http://editor.datatables.net/generator
 */

// Enable error reporting for debugging (remove for production)
error_reporting(E_ALL);
ini_set('display_errors', '1');

/*
 * Edit the following with your database connection options
 */
$sql_details = array(
    "type" => "Mysql",
    "user" => "root",
    "pass" => "",
    "host" => "localhost",
    "port" => "",
    "db"   => "ccrp_db",
    "dsn"  => "charset=utf8"
);

?>

The word “transaction” is not part of your code. You can debug any variable using var_dump() and then going backwards to where it came from.

Why would that matter? The error is coming from Editor.php, not from config.php.

The following ‘logic’ reproduces the errors/backtrace -

$p = ['some string value'];

function q($arr)
{
	foreach($arr as $value)
	{
		// there's some code here that is supposed to produce an 'editor' object in $value, but isn't and is using the original string value in $value
		
		$value->transaction();
	}
}

function r($arr){
	q($arr);
}

r($p);

If having/not-having a database connection triggers this error, either one, any, or all of these is the cause of the problem - the code has no error handling for database statements, the error handling is not stopping code execution (this is a follow-on error, not the real problem), the code is reusing variable(s) for different things (the input is a string, the output should be an object, the code should be using a different variable name for the output, not the original), or there’s no validation (or bad validation) from one step to the next (this could be due to a query that matches no data and should either result in the code not using the empty data at all or it should produce an ‘editor’ object with default/empty properties.)

Since we don’t have access to the full code base and your data, it’s up to you to determine why the value isn’t what it is supposed to be.

This link contains the Editor.php file from DataTables Editor: https://pastebin.com/UsPBTVyi

I cannot post it here as the post will be too long.

So, the error actually means that the _db property (in $this->_db) is a string. This gets set when you make an instance of the editor in your code. The example code looks like -

$editor = Editor::inst( $db, 'staff' );

Whatever you are doing, the 1st parameter is a string, not an instance of an expected database connection.

I figured that out quickly after I added that variable back in. Whoops…

Now, I don’t have any pagination in my database. I’ve called the pageLength attribute in my JS file, but it doesn’t appear to work:

/*
 * Editor client script for DB table members
 * Created by http://editor.datatables.net/generator
 */

(function($){

$(document).ready(function() {
	var editor = new $.fn.dataTable.Editor( {
		ajax: 'api/server_data.php',
		processing: true,
		serverSide: true,
		order: [],
		pageLength : 25,
		table: '#dataTable',
		fields: [		
			{
				"label": "Name:",
				"name": "name"
			},
			{
				"label": "Residential Address:",
				"name": "residential_address"
			},
			{
				"label": "Mailing Address:",
				"name": "mailing_address"
			},
			{
				"label": "Precinct:",
				"name": "precinct"
			},
			{
				"label": "Age:",
				"name": "age"
			},
			{
				"label": "Ethnicity:",
				"name": "ethnicity"
			},
			{
				"label": "Gender:",
				"name": "gender"
			},
			{
				"label": "Party:",
				"name": "party",
				"def": "REP"
			},
			{
				"label": "Race:",
				"name": "race"
			},
			{
				"label": "Phone:",
				"name": "phone"
			}
		]
	} );

	var table = $('#dataTable').DataTable( {
		dom: 'Bfrtip',
		processing: true,
		serverSide: true,
		order: [],
		pageLength: 25,
		ajax: 'api/server_data.php',
		columns: [		
			{
				"data": "name"
			},
			{
				"data": "residential_address"
			},
			{
				"data": "mailing_address"
			},
			{
				"data": "precinct"
			},
			{
				"data": "age"
			},
			{
				"data": "ethnicity"
			},
			{
				"data": "gender"
			},
			{
				"data": "party"
			},
			{
				"data": "race"
			},
			{
				"data": "phone"
			}
		],
		select: true,
		lengthChange: false,
		buttons: [
			{ extend: 'create', editor: editor },
			{ extend: 'edit',   editor: editor },
			{ extend: 'remove', editor: editor }
		]
	} );
} );
	
}(jQuery)); 

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.