Updating joined tables

Updating joined tables

Donny85Donny85 Posts: 10Questions: 0Answers: 0
edited April 2013 in Editor
I'm trying to update the value of fields from a joined table. The values that are submitted through POST match the updated values, but when returned, return to what they were prior to update. I'm assuming the single row edit join isn't being made, thus the values aren't updating? I am able to update the values of the primary table fine. I've looked at the join examples and can't seem to find any differential factors.

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


//to make the price look pretty (pricey!)
function formatPrice ($price) {
$numberStr = (string)$price;
$thousands = substr($numberStr, 0, 3);
$hundreds = substr($numberStr, 3, 6);
return "$" . $thousands . "," . $hundreds;
}


//return to the DB as numbers only
function stripPrice ($price) {
$noDollarSign = substr($price, 1, 7);
$noComma = str_replace(",", "", $noDollarSign);
return $noComma;
}


// 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, 'apartments' )
->fields(
Field::inst( 'id' )
->set( false ),
Field::inst( 'number' ),
Field::inst( 'level' ),
Field::inst( 'reservation_id' )
->set( false ),
Field::inst( 'price' )
->setFormatter( function ($val, $data, $field) {
return stripPrice($val);
} )
->getFormatter( function ($val, $data, $field) {
return formatPrice($val);
} ),
Field::inst( 'status' )
->setFormatter( function ($val, $data, $field) {
if ($val == "Available") return 0;
else if ($val == "Reserved") return 1;
else if ($val == "Sold") return 2;
else return 3;
} )
->getFormatter( function ($val, $data, $field) {
if ($val == 0) return "Available";
else if ($val == 1) return "Reserved";
else if ($val == 2) return "Sold";
else return "In Application";
} )
)
->join(
Join::inst( 'reservations', 'object' )
->join( 'reservation_id', 'id' )
->fields(
Field::inst( 'id' )
->set( false ),
Field::inst( 'is_active' ),
Field::inst( 'first_name' ),
Field::inst( 'last_name' ),
Field::inst( 'email' ),
Field::inst( 'phone' ),
Field::inst( 'initial_deposit' )
->setFormatter( function ($val, $data, $field) {
if ($val == "No") return 0;
else return 1;
} )
->getFormatter( function ($val, $data, $field) {
if ($val == 0) return "No";
else return "Yes";
} ),
Field::inst( 'has_ten_percent' )
->setFormatter( function ($val, $data, $field) {
if ($val == "No") return 0;
else return 1;
} )
->getFormatter( function ($val, $data, $field) {
if ($val == 0) return "No";
else return "Yes";
} ),
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' )
)
);

$out = $editor
->process($_POST)
->data();

echo json_encode( $out );
[/code]

Replies

  • Donny85Donny85 Posts: 10Questions: 0Answers: 0
    Gotcha!
    ->pkey( 'reservation_id' )
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    With the pkey it is working now? You cal also pass the pkey in as the third parameter to the `Editor` instance constructor: http://editor.datatables.net/docs/current/php/class-DataTables.Editor.html#___construct .

    Allan
  • Donny85Donny85 Posts: 10Questions: 0Answers: 0
    Yeah, using pkey() solved the issue thanks. Would it be possible for the join tutorial to incorporate the use of this method? It would be handy to highlight to speed up debugging for future users.
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    Yes - thanks for the suggestion - I will look at highlighting this option.

    Allan
This discussion has been closed.