Update a field based on the value of another
Update a field based on the value of another
I'm loving DataTables and have only encountered one complication. I have a scenario where a field is updated by the value of a group of three radio boxes. This works fine. However on edit/update, I wish to check this value, and based on it, update another field within the Database.
In this instance, I wish to update a field "reservation_id" to 0 if "status" is updated to "Available"
My current code is:
[code]
Editor::inst( $db, 'apartments' )
->fields(
Field::inst( 'number' ),
Field::inst( 'level' ),
Field::inst( 'price' ),
Field::inst( 'status' )
->setFormatter( function ($val, $data, $field) {
if ($val == "Available") return 0;
else if ($val == "Reserved") return 1;
else return 2;
} )
->getFormatter( function ($val, $data, $field) {
if ($val == 0) return "Available";
else if ($val == 1) return "Reserved";
else return "Sold";
} ),
Field::inst( 'test' )
->set("blah")
)
->join(
Join::inst( 'reservations', 'object' )
->join( 'id', 'apartment_id' )
->field(
Field::inst( 'first_name' ),
Field::inst( 'last_name' ),
Field::inst( 'email' ),
Field::inst( 'phone' ),
Field::inst( 'initial_deposit' ),
Field::inst( 'agent_company' ),
Field::inst( 'agent_name' ),
Field::inst( 'agent_email' ),
Field::inst( 'date' )
->getFormatter( 'Format::date_sql_to_format', 'D, d M y' )
)
)
->process( $_POST ) // The "process" method will handle data get, create, edit and delete requests from the client
->json();
[/code]
Thanks!
In this instance, I wish to update a field "reservation_id" to 0 if "status" is updated to "Available"
My current code is:
[code]
Editor::inst( $db, 'apartments' )
->fields(
Field::inst( 'number' ),
Field::inst( 'level' ),
Field::inst( 'price' ),
Field::inst( 'status' )
->setFormatter( function ($val, $data, $field) {
if ($val == "Available") return 0;
else if ($val == "Reserved") return 1;
else return 2;
} )
->getFormatter( function ($val, $data, $field) {
if ($val == 0) return "Available";
else if ($val == 1) return "Reserved";
else return "Sold";
} ),
Field::inst( 'test' )
->set("blah")
)
->join(
Join::inst( 'reservations', 'object' )
->join( 'id', 'apartment_id' )
->field(
Field::inst( 'first_name' ),
Field::inst( 'last_name' ),
Field::inst( 'email' ),
Field::inst( 'phone' ),
Field::inst( 'initial_deposit' ),
Field::inst( 'agent_company' ),
Field::inst( 'agent_name' ),
Field::inst( 'agent_email' ),
Field::inst( 'date' )
->getFormatter( 'Format::date_sql_to_format', 'D, d M y' )
)
)
->process( $_POST ) // The "process" method will handle data get, create, edit and delete requests from the client
->json();
[/code]
Thanks!
This discussion has been closed.
Replies
[code]
$post = $_POST;
if ( $post['action'] === 'create' || $post['action'] === 'edit' ) {
if ( $post['data']['status'] === 'Available' ) {
$post['data']['reservation_id'] = 0;
}
}
[/code]
Then feed $post rather than $_POST into the `process` method.
On the client-side you could do something similar with the onPreSubmit event - listen for that and then modify the data as required.
Regards,
Allan
Allan
[quote]Notice: Undefined index: reservations in ...Editor/Join.php on line 496[/quote]
When I set->(false), this is resolved, but obviously I can no longer update fields within the joined table.
I've tried set->(false) on all fields that won't be updated, but encounter the same error.
Note that not every "apartments" entry will pull data from the join and I only need to update a field within the joined table based on the same conditions previously highlighted.
Also, the data within the "apartments" table gets updated, but I still receive the "An error has occurred - Please contact the system administrator" error.
If I use $reservationID = $post['data']['reservation_id'], I get the error "Undefined index: resevation_id in"
Allan