I have a class ‘listing’ which has the following fields:
static $db = array(
...
'Left' => 'Varchar',
'Top' => 'Varchar'
...
);
I am grabbing these to display over a map with:
function getListings(){
$sqlQuery = new SQLQuery();
$sqlQuery->setFrom('ListingCategory_Listings');
$sqlQuery->selectField('STLC.Title AS cTitle, STLL.Title AS lTitle, Listing_Live.ID AS lID, ListingCategory.ID as cID, Phone, Email, Website, Filename, Top, Listing_Live.Left, STLC.ID AS catID');
$sqlQuery->addLeftJoin('Listing_Live', '"ListingCategory_Listings"."ListingID" = "Listing_Live"."ID"');
$sqlQuery->addLeftJoin('SiteTree_Live', '"Listing_Live"."ID" = "STLL"."ID"', 'STLL');
$sqlQuery->addLeftJoin('ListingCategory', '"ListingCategory_Listings"."ListingCategoryID" = "ListingCategory"."ID"');
$sqlQuery->addLeftJoin('SiteTree_Live', '"ListingCategory"."ID" = "STLC"."ID"', 'STLC');
$sqlQuery->addLeftJoin('File', '"ListingCategory"."IconID" = "File"."ID"');
$sqlQuery->addWhere('"Listing_Live"."ID" IS NOT NULL');
$sqlQuery->setOrderBy('ListingCategory.ID', 'ASC');
$result = $sqlQuery->execute();
$dataObject = new ArrayList();
foreach($result as $row) {
$dataObject->push(new ArrayData($row));
}
return $dataObject;
}
I have a function to update the left and top values of the objects (fired via AJAX by dragging and dropping an instance of the object on the front end):
function updatePoi(){
$left = $_POST['left'];
$top = $_POST['top'];
$id = $_POST['ID'];
if ($poi = DataObject::get_one('Listing', '"Listing"."ID" = '.$id)){
$poi->Left = $left;
$poi->Top = $top;
$poi->write();
return 'success';
}else{
return 'error';
}
}
These are definitely being updated, as on page refresh, the objects are reflecting the updated Top and Left values, however I am showing these fields in the CMS but they are showing blank here. Also, when I save the object in the CMS, the Left and Top values are cleared and I have to reposition them again in the front end.
Does anyone understand why this is happening?
UPDATE: It seems that the write() only updates the Listing_Live table, not the Listing table how can I get it to update both without doing this with raw SQL?
As a quick fix, I have changed the update function to:
I’d be happy to hear of a tidier way of doing this.