2015-10-13 23 views
2

我使用卡桑德拉和使用Datastax PHP CQL。datastax卡桑德拉查詢結果解碼在PHP

考慮下面的查詢,我應該怎麼解碼的結果?

$cql = SELECT * FROM company WHERE userid = 1001 ORDER BY gpsTime DESC LIMIT 1;       
$statement = new Cassandra\SimpleStatement($cql); 
$result = $this->session->execute($statement); 

我試着用解碼:

foreach($result as $row){ 
    // Get values from row and add to array 
    array_push($allVehicleInfo,array(
     'userID' => $row['userid']->value, 
     'gpsTime' => $row['gpstime']->seconds, 
     'latitude' => $row['latitude']->value) 
    ); 
} 

但是,我得到一個錯誤:

Message: Undefined property: Cassandra\Decimal::$value


我的表架構是:

$cql = "CREATE TABLE " company " (
    userID bigint, 
    userName ascii, 
    latitude decimal, 
    longitude decimal, 
    accuracy float, 
    altitude float, 
    gpsTime timestamp); 

回答

1

value是一個函數,而不是財產,所以你要做的$row['latitude']->value()

0

亞歷克斯說,在你的例子value不是屬性。相反,請檢查你試圖解碼類的文檔。 The documentation can be found here

如果您不確定要處理的課程,則可以使用內部功能get_class() - see get_class() manual

一旦你知道你與你打交道什麼類可以檢查DataStax PHP驅動程序文檔,看看哪些功能,您需要調用來檢索正確的值。

例如,如果你正在處理一個Cassandra\Timestamp類,你就必須調用time()函數來獲取時間爲intSee Timestamp documentation

在你的情況,你可能有Float處理,所以檢查the Float documentation,看到你需要調用函數value()檢索Cassandra\Float實例的float值。


代碼解決方案:

foreach($result as $row){ 
    // Get values from row and add to array 
    array_push($allVehicleInfo,array(
     'userID' => $row['userid']->value(), 
     'gpsTime' => $row['gpstime']->seconds, 
     'latitude' => $row['latitude']->value()) 
    ); 
}