datastax卡桑德拉查询结果解码在PHP

问题描述:

我使用卡桑德拉和使用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); 

value是一个函数,而不是财产,所以你要做的$row['latitude']->value()

亚历克斯说,在你的例子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()) 
    ); 
}