Select Value Not Updating
Select Value Not Updating
Hi All,
I have the following code and it's almost perfect apart from when a user changes the select value, this doesn't reflect in the database. Everything else updates as it should do, can anyone see where I have gone wrong here?
Thanks
$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
ajax: "../../includes/ajax/load_contracts.php",
table: "#contract",
idSrc: 'contract.contractID',
fields: [ {
label: "Contract Number:",
name: "contract.contractNumber"
}, {
label: "Contract Start:",
name: "contract.contractStart"
}, {
label: "Contract Term:",
name: "contract.contractTerm"
}, {
label: "Fleet:",
name: "fleet[].fleetID",
type: "select"
}
]
});
$('#contract').DataTable( {
dom: "Bfrtip",
ajax: {
url: "../../includes/ajax/load_contracts.php",
type: 'POST'
},
columns: [
{ data: "contract.contractID" },
{ data: "contract.contractNumber" },
{ data: "contract.contractStart" },
{ data: "contract.contractTerm" },
{ data: "fleet", render: "[, ].name" }
],
select: true,
buttons: [
{ extend: "create", editor: editor },
{ extend: "edit", editor: editor },
{ extend: "remove", editor: editor }
]
} );
} );
```php
<?php
// DataTables PHP library
include($_SERVER['DOCUMENT_ROOT'] . '/includes/datatables/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\Upload,
DataTables\Editor\Validate;
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'contract', 'contract.contractID' )
->fields(
Field::inst( 'contract.contractID' ),
Field::inst( 'contract.contractNumber' ),
Field::inst( 'contract.contractStart' ),
Field::inst( 'contract.contractTerm' )
)
->join(
Mjoin::inst( 'fleet' )
->link( 'contract.contractID', 'contract_customer.contractID' )
->link( 'fleet.fleetID', 'contract_customer.customerID' )
->fields(
Field::inst( 'fleetID' )
->options( 'fleet', 'fleetID', 'name' ),
Field::inst( 'name' )
)
)
->process( $_POST )
->json();
This question has an accepted answers - jump to answer
Answers
Bump
Hi,
Thanks for the code. I think the issue might just be that you need to add
multiple: true
into the Editor field object for the select field. For example just addmultiple: true
immediately after line 18.At the moment the select list will be shown as a single select item only, but the way your PHP code is structured suggests that multiple items can be selected - is that correct?
Thanks,
Allan
Hi Allan,
Many thanks for the reply, adding multiple: true does indeed work but I only want one item to be selected, as only one contract can be assigned to one fleet.
What should I change here?
Thanks.
I see - thanks. You probably don't want to use an Mjoin on the server-side then. Is there a
contract.fleetID
parameter in thecontract
table that references the foreign key, or is there a separate table that you need to modify the value of? I see thecontract_customer
table - is that purely a link table (two columns, both foreign keys)? Was it put in explicitly for Editor, or is that something that is required in your database schema for other things?With a 1:1 mapping, I would normally suggest having a
fleetID
in yourcontract
table here, but if there is another hop, that is possible.Allan
Hi Allan,
There isn't a contract.fleetID in the contract table and yes the contract_customer is purely a link table between the contract and fleet tables which is required for other things.
Below is the database schema which I hope helps:
Thanks.
Thanks for the schema. it shows that
contract_customer
isn't actually just a link table, but has a lot of meta information as well. Mjoin isn't suitable for that as it deletes and adds rows rather than updating (this is for Mjoin specifically, a normal table does of course do an update).So what I think you need are two left joins - one to the link table and one to the fleet table. Have a read over this thread which discusses basically the same thing - updating the joined table.
Allan
Hi Allan,
Thanks for the explanation, I have used two joins as you have suggested but now I get the following error when using
multiple:true
in the select and when I don't use this, the field doesn't update as it should do.Below is the updated code:
Many thanks
Hi,
So the
multiple:true
probably shouldn't be used now since it is 1:1 - no need to be able to select multiple items.What I think you want, in PHP is:
Note that I moved the options to the
contract_customer.fleetID
field - that is the column you want to edit the value of, so that's the one the options should be applied to.In Javascript use:
So basically you want to edit the value of
contract_customer.fleetID
(with a value fromfleet.fleetID
), and you need to submit the primary key for the joined table you want to edit (contract_customer.contractCustomerID
in this case).Does that clarify what is needed?
Regards,
Allan
Hi Allan,
That's perfect, I now have it working exactly as I need it to
Many thanks for all your help with this.