我需要在Symfony2命令中保留反序列化的对象whit Doctrine:
/* create object */
$e = new Event();
$u = $em->getRepository('TestWebBundle:User')->findOneByUsername('test_user');
$e->setUser($u);
$e->setCompany($u->getCompany());
$e->setType(Event::TYPE_COMPANY);
$e->setTime(new \DateTime());
/* serialize */
$es = serialize($e);
/* unserialize */
$esu = unserialize($es);
/* try to store in db */
$em->persist($esu); // error appears here
$em->flush();
$ em是EntityManager,一切正常,包括序列化(我认为),但出现警告
[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: in_array() expects parameter 2 to be array, null given
并且对象本身不保存。如果我试图坚持$ e它的工作正确。知道什么是错的吗?
答案 0 :(得分:-1)
我通过实现自己的序列化和反序列化解决了这个问题:
function cserialize($o,$raw=false){
static $fn=__FUNCTION__;
$otype = strtolower(gettype($o));
// Simple types
if(in_array($otype,array('null','boolean','integer','float','string','double'))){
$r = array(
'type'=>'raw',
'value'=>$o
);
if($raw)return $r;
return serialize($r);
}
//
if($otype == 'object'){
$oclass = get_class($o);
} else {
$oclass = null;
}
// Arrays
if(is_array($o) || $oclass == 'Doctrine\Common\Collections\ArrayCollection' ){
$t = array();
foreach ($o as $element)
$t[] = $fn($element,true);
$r = array(
'type'=>'array',
'value'=>$t
);
if($raw)return $r;
return serialize($r);
}
// Objects
if($otype == 'object'){
$isEntity = (strpos($oclass, '\\Entity\\') !== false);
if($isEntity && $o->getId()!==null){
// Entity whit id - keep just id
$repository = explode('\\Entity\\',$oclass);
$repository[0] = implode('',explode('\\',$repository[0]));
$repository[1] = implode('',explode('\\',$repository[1]));
$repository = implode(':',$repository);
$repository = str_replace('Proxies__CG__','',$repository);
$r = array(
'type'=>'entity-stored',
'repository'=>$repository,
'id'=>$o->getId()
);
if($raw)return $r;
return serialize($r);
}
if(!$isEntity){
// Some kind of object - use normal serialization
$r = array(
'type'=>'raw',
'value'=>$o
);
if($raw)return $r;
return serialize($r);
} else {
$t = array();
foreach (get_class_methods($o) as $method) {
if(strncmp('get',$method,3)!=0 ||
$method == 'getAsFlatArray' ||
$method == 'getId'
)
continue;
$val = $o->$method();
$name = 'set'.substr($method,3);
if($val !== null)
$t[$name] = $fn($val,true);
}
$r = array(
'type'=>'entity-new',
'class'=>$oclass,
'value'=>$t
);
if($raw)return $r;
return serialize($r);
}
}
throw new Exception("Error in serializer", 1);
}
function cunserialize($o,$em,$raw=false){
static $fn = __FUNCTION__;
if(!$raw)$o=unserialize($o);
switch ($o['type']) {
case 'array':
$t=array();
foreach ($o['value'] as $element)
$t[] = cunserialize($element,$em,$raw);
return $t;
case 'entity-new':
$class = '\\'.$o['class'];
$r = new $class();
foreach ($o['value'] as $method => $value)
$r->$method($fn($value,$em,true));
return $r;
case 'entity-stored':
return $em->getRepository($o['repository'])->findOneById($o['id']);
case 'raw';
return $o['value'];
default:
throw new Exception("Error in unserializer", 1);
}
}
现在我可以致电:
$es = cserialize($e);
$esu = cunserialize($es,$em);
问题在于睡眠和唤醒属性。