dongshen2903 2015-09-27 19:28
浏览 49

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

Please excuse the long post - I've tried to be as succinct as possible, whilst giving as much detail as I can ... and I've historically been a procedural PHP programmer, now getting my feet wet with objects.

I'm working on a test project where I'm using a 3rd party class (loaded via phar) to capture a stream from an online server. The online server posts events, I capture (using "capture.php") each event with the 3rd party class, serialize the data and then store it to a MySQL database as a BLOB field.

//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 "
SQL is $sql
";    
});

In a different script ("read.php" - I know - I'm VERY creative in script naming) I ensure that I have the include files to the class definition files, pick up the first entry in the database, pull the BLOB data and then unserialize it. At this point I'm testing the variable type in read.php and the variable is an object:

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 "
event is " . gettype($event) . "
";
print_r($event);

And this is the result I get:

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
)

Ok - so far so good, IMHO. This certainly appears to be an object, all the data certainly appears to have been stored correctly (rawCapture.php is another script which also polls the data and var_dumps the same events out to a text file, so that I can "compare" and ensure that everything is being recorded correctly).

However, now I want to deal with some of the object data and this is where I'm having issues. For example, I'm trying to obtain the value of the Uniqueid, but using $id = $event->Uniqueid; I get the following error PHP Notice: Undefined property: PAMI\Message\Event\NewchannelEvent::$Uniqueid in /home/dave/objRead.php on line 69

Now I'm stuck. I've tried checking that $event is in fact an object, using get_object_vars():

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

and I'm getting my own error code "objVars doesn't appear to be an object?!".

Can anyone give me some advice on what on earth I'm doing wrong??

Also - I've tried to keep indentation in the code sections above but it wouldn't let me post without removing them. I've also amended my OP to show that I'm including the 3rd party code so that the class definitions are loaded, and the line that I use to unserialize the data from the MySQL table.

  • 写回答

1条回答 默认 最新

  • doutuo7609 2015-09-27 19:58
    关注

    Have you tried changing the case of Uniqueid in $id = $event->Uniqueid; to lower case?
    So it would be $id = $event->uniqueid;

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向