one of joined tables's field is not being updated
one of joined tables's field is not being updated
tatevikz
Posts: 12Questions: 3Answers: 0
Hi. So I have 4 joined tables which some fields need to be updated. All the fields are being edited successfully except the one. The products -> products_price.
Could you please help me to figure out what I'm doing wrong.
JS part
var editor; // use a global for the submit and return data rendering in the examples
$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
ajax: "ajax/get_table_values.php",
table: "#myTable",
fields: [
{
label: "Categories Name:",
name: "categories_description.categories_name"
}, {
label: "Categories Heading Title:",
name: "categories_description.categories_heading_title"
}, {
label: "Categories Description:",
name: "categories_description.categories_description"
}, {
label: "Concert Venue:",
name: "categories_description.concert_venue"
}, {
label: "Date ID:",
name: "categories.date_id",
type: "datetime",
def: function () { return new Date(); },
format: 'YYYY-MM-DD h:mm',
opts: {
minutesIncrement: 5
}
}, {
label: "Products Price:",
name: "products.products_price"
}, {
label: "Categories Status:",
name: "categories.categories_status",
type: "radio",
options: [
{ label: "Uncheck", value: 0 }, //1
{ label: "Check", value: 1 }, //1
]
}
]
} );
$('#myTable').on( 'click', 'tbody td:not(:first-child)', function (e) {
editor.inline( this, {
onBlur: 'submit'
} );
} );
$('#myTable').DataTable( {
dom: "Bfrtip",
ajax: {
url: 'ajax/get_table_values.php',
type: 'POST'
},
order: [[ 1, 'asc' ]],
columns: [
{ data: "categories_description.categories_name" },
{ data: "categories_description.categories_heading_title" },
{ data: "categories_description.categories_description" },
{ data: "categories_description.concert_venue" },
{ data: "categories.date_id" },
{ data: "products.products_price" },
{ data: "categories.categories_status",
orderable: false,
render: function ( data, type, row ) {
if ( type === 'display' ) {
return '<input type="checkbox" class="editor-categories_status">';
}
return data;
},
render: function (val, type, row) {
return val == 0 ? 0 : 1;
}
}
],
select: {
style: 'os',
selector: 'td:not(:last-child)'
},
buttons: [
{ extend: "edit", editor: editor },
],
} );
} );
HTML part
<table id="myTable" class="display" style="width:100%">
<thead>
<tr>
<th>Categories Name</th>
<th>Categories Heading Title</th>
<th>Categories Description</th>
<th>Concert Venue</th>
<th>Concert Date</th>
<th>Products Price</th>
<th>Categories Status</th>
</tr>
</thead>
</table>
PHP part
<?php
/*
* Example PHP implementation used for the index.html example
*/
// DataTables PHP library
include( "../extensions/lib/DataTables.php" );
// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate,
DataTables\Editor\ValidateOptions;
Editor::inst( $db, 'categories_description', 'categories_id' )
->field(
Field::inst( 'categories_description.categories_name' ),
Field::inst( 'categories_description.categories_heading_title' ),
Field::inst( 'categories_description.categories_description' ),
Field::inst( 'categories_description.concert_venue' ),
Field::inst( 'categories.date_id' )
->getFormatter( Format::dateSqlToFormat('Y-m-d H:i' ) ),
Field::inst( 'products.products_price' ),
Field::inst( 'categories.categories_status' )
->setFormatter( function ( $val, $data, $opts ) {
return ! $val ? 0 : 1;
} )
)
->leftJoin( 'categories', 'categories.categories_id', '=', 'categories_description.categories_id' )
->leftJoin( 'products_to_categories', 'categories_description.categories_id', '=', 'products_to_categories.categories_id')
->leftJoin( 'products', 'products_to_categories.products_id', '=', 'products.products_id' )
->process($_POST)
->json();
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
If you want to update a joined row, you need to include its primary key in the data submitted to the server. So you'll need
products.id
as a field in the PHP and also as ae-field
hidden in the Editor Javascript.That way the libraries can uniquely identify the row to update in the joined table.
Allan
Thanks you so much! You just saved me!