Updating joined tables
Updating joined tables
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]
[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]
This discussion has been closed.
Replies
->pkey( 'reservation_id' )
Allan
Allan