使用write()更新的DataObject未反映在CMS中

时间:2012-10-16 20:50:02

标签: silverstripe

我有一个班级'列表',其中包含以下字段:

static $db = array(
            ...
            'Left' => 'Varchar',
            'Top' => 'Varchar'
            ...
    );

我抓住这些以显示在地图上:

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;
    }

我有一个函数来更新对象的左边和顶部值(通过在前端拖放对象的实例来通过AJAX触发):

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';
        }
    }

这些肯定正在更新,因为在页面刷新时,对象反映了更新的Top和Left值,但是我在CMS中显示这些字段,但它们在此处显示为空白。此外,当我在CMS中保存对象时,Left和Top值被清除,我必须在前端再次重新定位它们。

有谁知道为什么会这样?

更新:似乎write()只更新了Listing_Live表,而不是List表如何在不使用原始SQL的情况下更新它们?

2 个答案:

答案 0 :(得分:1)

我建议使用

$poi->Top = $top;
$poi->write(); 
$poi->publish("Live", "Stage");

答案 1 :(得分:0)

作为快速解决方法,我已将更新功能更改为:

function updatePoi(){
        $left = $_POST['left'];
        $top = $_POST['top'];
        $id = $_POST['ID'];
        DB::query('UPDATE "Listing" SET "Left"='.$left.', "Top"='.$top.' WHERE "ID"='.$id);
        DB::query('UPDATE "Listing_Live" SET "Left"='.$left.', "Top"='.$top.' WHERE "ID"='.$id);
        return 'success';
    }

我很高兴听到这样做的更为简洁的方法。