将对象存储到数据库,然后再次检索它


Storing an object to database and then retrieving it back again

请原谅我的帖子太长了——我已经尽可能简明扼要,同时给出了尽可能多的细节……我以前是一个过程式PHP程序员,现在开始接触对象了。

我正在进行一个测试项目,我使用第三方类(通过phar加载)从在线服务器捕获流。在线服务器发布事件,我用第三方类捕获(使用"capture.php")每个事件,序列化数据,然后将其作为BLOB字段存储到MySQL数据库中。

//capture.php
// include phar file
require_once 'PAMI-1.72.phar';
// set include path to have pami's phar first
ini_set('include_path', implode(PATH_SEPARATOR, array('phar://pami.phar', ini_get('include_path'))));
use PAMI'Client'Impl'ClientImpl as PamiClient;
use PAMI'Message'Event'EventMessage;
use PAMI'Listener'IEventListener;
$pamiClient = new PamiClient($pamiClientOptions);
// Open the connection
$pamiClient->open();
$pamiClient->registerEventListener(function (EventMessage $event) {
// Following line dumps the event to a text file in rawCapture.php
//var_dump($event);
$mysqli = new mysqli($server, $user, $pass, $db);
if ($mysqli->connect_error) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
exit;
}
$serializedData = $mysqli->real_escape_string(serialize($event));
$sql = 'insert into obj (data) values ("' . $serializedData . '")';
echo "'r'nSQL is $sql'r'n";    
});

在不同的脚本("read.php"-我知道-我在脚本命名非常有创意)我确保我有包含文件到类定义文件,拿起数据库中的第一个条目,拉BLOB数据,然后反序列化它。此时,我正在测试read。php中的变量类型变量是一个对象:

require_once 'PAMI-1.72.phar';
// set include path to have pami's phar first
ini_set('include_path', implode(PATH_SEPARATOR, array('phar://pami.phar', ini_get('include_path'))));
use PAMI'Client'Impl'ClientImpl as PamiClient;
use PAMI'Message'Event'EventMessage;
use PAMI'Listener'IEventListener;
/* All the MySQL stuff to get the $data from the db */
$event = unserialize($data);
echo "'r'nevent is " . gettype($event) . "'r'n";
print_r($event);

我得到的结果是:

event is object
PAMI'Message'Event'NewchannelEvent Object
(
[rawContent:protected] => Event: Newchannel
Privilege: call,all
Channel: Local/s@tc-maint-00006a43;2
ChannelState: 4
ChannelStateDesc: Ring
CallerIDNum: 
CallerIDName: 
AccountCode: 
Exten: s
Context: tc-maint
Uniqueid: 1443368640.57856
[lines:protected] => Array
(
)
[variables:protected] => Array
(
)
[keys:protected] => Array
(
[event] => Newchannel
[privilege] => call,all
[channel] => Local/s@tc-maint-00006a43;2
[channelstate] => 4
[channelstatedesc] => Ring
[calleridnum] => 
[calleridname] => 
[accountcode] => 
[exten] => s
[context] => tc-maint
[uniqueid] => 1443368640.57856
)
[createdDate:protected] => 1443368636
)

好吧-到目前为止还不错,恕我直言。这当然似乎是一个对象,所有的数据当然似乎已经被正确存储(rawCapture.php是另一个脚本,它也轮询数据和var_dumps 相同的事件到文本文件,这样我就可以"比较",并确保一切都被正确记录)。

然而,现在我想处理一些对象数据,这就是我有问题的地方。例如,我试图获得唯一id的值,但使用$id = $event->Uniqueid;,我得到以下错误PHP Notice: Undefined property: PAMI'Message'Event'NewchannelEvent::$Uniqueid in /home/dave/objRead.php on line 69

现在我被卡住了。我已经尝试检查$event实际上是一个对象,使用get_object_vars():

$objVars = get_object_vars($event);
if(is_null($objVars)){
echo "'r'nobjVars doesn't appear to be an object?!'r'n";
} else {
if(is_array($objVars)){
print_r($objVars);
} else {
echo "'r'nobjVars isn't an array!!'r'n";
}
}

和我得到我自己的错误代码"objVars不似乎是一个对象?!"。

谁能给我一些建议,我到底做错了什么?

也-我试图保持缩进在上面的代码部分,但它不会让我张贴没有删除它们。我还修改了我的OP,以显示我正在包括第三方代码,以便加载类定义,以及我用于从MySQL表unserialize数据的行。

您是否尝试将$id = $event->Uniqueid;中的Uniqueid更改为小写?
所以应该是$id = $event->uniqueid;