Here u see, i added a new user to database, i want to do something after adding a new user, such as storing user data to redis and so on.. I am thinking Listener
to do that .
The question is :
I got the entity in postPersist. but it's an object . I want to convert it to an array to store it.
public function postPersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$em = $args->getEntityManager();
// var_dump($em->getRepository('AppBundle:YaUser')->find($entity->getUid()));
if($entity instanceof YaUser){
}
}
I can't use get_object_vars
btw. Because the entity
is private then i get empty
THX:)
You should use the JMS Serializer
To serialize and object:
$serializer = JMS\Serializer\SerializerBuilder::create()->build();
$serializer->serialize($yourObject, 'json');
And to deserialize:
$serializer = JMS\Serializer\SerializerBuilder::create()->build();
$object = $serializer->deserialize($jsonData, 'YourNamespace\YourObject', 'json');
Here my code. It looks strange.
if($entity instanceof YaUser) {
$help = $this->container->get('help');
$uinfo = [];
foreach(get_class_methods($entity) as $k => $v) {
if(preg_match('/^get.*$/', $v)){
$uinfo_key = strtolower(str_replace('get', '', $v));
$uinfo[$uinfo_key] = call_user_func_array([$entity, $v], []);
}
}
if($uinfo){
$key = $help->redisKey('uinfo').$uinfo['uid'];
$u_old = $help->getRedis($key, $help::REDIS_ARRAY);
$uinfo = array_merge((array)$u_old, (array)$uinfo);
$help->saveRedis($key, $uinfo);
}
}