Quantcast
Channel: Recent Discussions — DataTables forums
Viewing all articles
Browse latest Browse all 82140

Datatables Editor cannot create new entry with join tables

$
0
0
Hello,

I`ve created a simple join through that document
http://editor.datatables.net/release/DataTables/extras/Editor/examples/join.html

When trying to create a new entry i`m getting an error:
Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`ci_tools`.`dashboard_task`, CONSTRAINT `FRG_KEY_PRT` FOREIGN KEY (`priority`) REFERENCES `dashboard_priority` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)'

PHP
<?php

/*
 * Editor server script for DB table dashboard_task
 * Automatically generated by http://editor.datatables.net/generator
 */

// DataTables PHP library
include( "lib/DataTables.php" );

// Alias Editor classes so they are easy to use
use
	DataTables\Editor,
	DataTables\Editor\Field,
	DataTables\Editor\Format,
	DataTables\Editor\Join,
	DataTables\Editor\Validate;


// Build our Editor instance and process the data coming from _POST
$editor = Editor::inst( $db, 'dashboard_task' )
	->fields(
		Field::inst( 'task' )
			->validator( 'Validate::required' ),
		Field::inst( 'priority' ),
		Field::inst( 'owner' )
			->validator( 'Validate::required' ),
		Field::inst( 'sdate' )
			->validator( 'Validate::dateFormat_required', 'D, j M Y' )
			->getFormatter( 'Format::date_sql_to_format', 'D, j M Y' )
			->setFormatter( 'Format::date_format_to_sql', 'D, j M Y' ),
		Field::inst( 'ddate' )
			->validator( 'Validate::dateFormat_required', 'D, j M Y' )
			->getFormatter( 'Format::date_sql_to_format', 'D, j M Y' )
			->setFormatter( 'Format::date_format_to_sql', 'D, j M Y' ),
		Field::inst( 'adate' )
			->getFormatter( 'Format::date_sql_to_format', 'D, j M Y' )
			->setFormatter( 'Format::date_format_to_sql', 'D, j M Y' ),
		Field::inst( 'status' ),
		Field::inst( 'quarter' ),
		Field::inst( 'remarks' )
	)
    ->join(
        Join::inst( 'dashboard_priority', 'object' )
            ->join( 'priority', 'id' )
            ->field(
                Field::inst( 'id' )->validator( 'Validate::required' ),
                Field::inst( 'name' )
            ),
		Join::inst( 'dashboard_status', 'object' )
            ->join( 'status', 'id' )
            ->field(
                Field::inst( 'id' )->validator( 'Validate::required' ),
                Field::inst( 'name' )
            )
    );
 
// 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 department details
    $out['dashboard_priority'] = $db
        ->select( 'dashboard_priority', 'id as value, name as label' )
        ->fetchAll();
	$out['dashboard_status'] = $db
        ->select( 'dashboard_status', 'id as value, name as label' )
        ->fetchAll();

}

// Send it back to the client
echo json_encode( $out );


JavaScript
/*
 * Editor client script for DB table dashboard_task
 * Automatically generated by http://editor.datatables.net/generator
 */

(function($){

$(document).ready(function() {
	var editor = new $.fn.dataTable.Editor( {
		"ajaxUrl": "php/table.dashboard_task.php",
		"domTable": "#dashboard_task",
		"fields": [
			{
				"label": "Task",
				"name": "task",
				"type": "text"
			},
			{
				"label": "Priority",
				"name": "dashboard_task.priority",
				"type": "select"
			},
			{
				"label": "Owner",
				"name": "owner",
				"type": "text"
			},
			{
				"label": "Start Date",
				"name": "sdate",
				"type": "date",
				"dateFormat": "D, d M yy",
				"dateImage": "images\/calender.png"
			},
			{
				"label": "Due Date",
				"name": "ddate",
				"type": "date",
				"dateFormat": "D, d M yy",
				"dateImage": "images\/calender.png"
			},
			{
				"label": "Actual Date",
				"name": "adate",
				"type": "date",
				"dateFormat": "D, d M yy",
				"dateImage": "images\/calender.png"
			},
			{
				"label": "Status",
				"name": "dashboard_task.status",
				"type": "select"
			},
			{
				"label": "Quarter",
				"name": "quarter",
				"type": "text"
			},
			{
				"label": "Remarks",
				"name": "remarks",
				"type": "text"
			}
		]
	} );

	$('#dashboard_task').dataTable( {
		"sDom": "<'row-fluid'<'span6'T><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
		"sAjaxSource": "php/table.dashboard_task.php",
		"aoColumns": [
			{
				"mData": "task"
			},
			{
				"mData": "dashboard_priority.name" , "sClass": "center"
			},
			{
				"mData": "owner" , "sClass": "center"
			},
			{
				"mData": "sdate" , "sClass": "center"
			},
			{
				"mData": "ddate" , "sClass": "center"
			},
			{
				"mData": "adate" , "sClass": "center"
			},
			{
				"mData": "dashboard_status.name"
			},
			{
				"mData": "quarter"
			},
			{
				"mData": "remarks"
			}
		],
		"oTableTools": {
			"sRowSelect": "multi",
			"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
            editor.field('dashboard_task.priority').update( json.dashboard_priority );
            editor.field('dashboard_task.status').update( json.dashboard_status );
        }
	} );
	
} );

}(jQuery));



DataTables debugger:
http://debug.datatables.net/ukoqoq

Viewing all articles
Browse latest Browse all 82140

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>