Update a field based on the value of another

Update a field based on the value of another

Donny85Donny85 Posts: 10Questions: 0Answers: 0
edited February 2013 in Editor
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!

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    If you want to do this on the server-side, I'd suggest something like this:

    [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
  • Donny85Donny85 Posts: 10Questions: 0Answers: 0
    Thanks, that works. I now also want to update a field within reservations, "is_active", only when a reservation exists for the particular apartments entry and its status is being made available again. I was having errors with editing when I added the join, so I added [code]->set( false )[/code] with the join, but now this prevents me from editing data within the reservations table...
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    What were the errors you were getting? If you want to be able to edit the joined table, then yes, you would need to remove the `->set( false )` from the join instance. You might need to add `set->(false)` to some of the fields if you are only editing some of the fields though.

    Allan
  • Donny85Donny85 Posts: 10Questions: 0Answers: 0
    edited February 2013
    When I try to edit, I get the following error:
    [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.
  • Donny85Donny85 Posts: 10Questions: 0Answers: 0
    Sorry, one more thing, how can I set a variable to $post['data']['reservation_id']?
    If I use $reservationID = $post['data']['reservation_id'], I get the error "Undefined index: resevation_id in"
  • Donny85Donny85 Posts: 10Questions: 0Answers: 0
    Never mind, I updated via a separate AJAX call with the "onEdit" function :)
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Fair enough. I think I'd need to see the working system to be able to figure out exactly what is going on with the Join, but if you are happy with your solution, that's great :-)

    Allan
This discussion has been closed.