Doctrine实体到Json使用GetSetMethodNormalizer返回致命错误 [英] Doctrine entity to Json using GetSetMethodNormalizer return fatal error
问题描述
我在yaml文件中生成我的实体。
现在我遇到一个问题,将我的实体Doctrine转换成Json格式(为了通过AJAX使用它)。
这里是使用的代码: p>
$ doctrineobject = $ this-> entityManager-> getRepository('\Entity\MasterProduct') - > find $这 - > _request-> ID);
$ serializer = new \Symfony\Component\Serializer\Serializer(array(new Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer()),array('json'=> new Symfony\Component\Serializer\Encoder\JsonEncoder()));
$ reports = $ serializer-> serialize($ doctrineobject,'json');
以下是我得到的回报:
致命错误:达到最大功能嵌套级别'100',中止!在/Users/Sites/library/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php第185行
问题似乎与以下相同:
http://comments.gmane.org/gmane.comp.php。 symfony.symfony2 / 2659
但是没有提出正确的解决方案。
任何想法如何我可以做吗?
干杯
问题通过写我自己的GetSetNormalizer我的课。在分类中定义静态变量
class LimitedRecursiveGetSetMethodNormalizer扩展GetSetMethodNormalizer
{
public static $ limit = 2;
/ **
* {@inheritdoc}
* /
public function normalize($ object,$ format = null)
{
$ reflectionObject = new \ReflectionObject($ object);
$ reflectionMethods = $ reflectionObject-> getMethods(\ReflectionMethod :: IS_PUBLIC);
$ attributes = array();
foreach($ reflectionMethods as $ method){
if($ this-> isGetMethod($ method)){
$ attributeName = strtolower(substr($ method-> name,3 ));
$ attributeValue = $ method-> invoke($ object);
if(null!== $ attributeValue&!is_scalar($ attributeValue)& 0){
LimitedRecursiveGetSetMethodNormalizer :: $ limit--;
$ attributeValue = $ this-> serializer-> normalize($ attributeValue,$ format);
LimitedRecursiveGetSetMethodNormalizer :: $ limit ++;
}
$ attributes [$ attributeName] = $ attributeValue;
}
}
返回$属性;
}
/ **
*检查方法的名称是否为。*,可以无参数调用。
*
* @param ReflectionMethod $方法检查
* @return布尔值是否为一个getter。
* /
私有函数isGetMethod(\ReflectionMethod $ method)
{
return(
0 === strpos($ method-> name,'get ')&
3< strlen($ method-> name)&&
0 === $ method-> getNumberOfRequiredParameters()
);
}
}
和使用
LimitedRecursiveGetSetMethodNormalizer :: $ limit = 3;
$ serializer = new Serializer(array(new LimitedRecursiveGetSetMethodNormalizer()),array('json'=> new
JsonEncoder()))
$ response = new Response($ serializer-> serialize($ YOUR_OBJECT,'json'));
I am using Doctrine 2 and Zend framework since a few days. I am generating my entities across yaml files. Now I met an issue to convert my entities Doctrine into Json format (in order to use it through AJAX).
Here is the code used :
$doctrineobject = $this->entityManager->getRepository('\Entity\MasterProduct')->find($this->_request->id);
$serializer = new \Symfony\Component\Serializer\Serializer(array(new Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer()), array('json' => new Symfony\Component\Serializer\Encoder\JsonEncoder()));
$reports = $serializer->serialize($doctrineobject, 'json');
below is the return I get :
Fatal error: Maximum function nesting level of '100' reached, aborting! in /Users/Sites/library/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php on line 185
the issue seems to be the same than here : http://comments.gmane.org/gmane.comp.php.symfony.symfony2/2659
but there is not proper solution proposed.
Any idea how can I do it ?
Cheers
I solved the same problem by writing my own GetSetNormalizer my class. Defined static variable in a class for branching
class LimitedRecursiveGetSetMethodNormalizer extends GetSetMethodNormalizer
{
public static $limit=2;
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null)
{
$reflectionObject = new \ReflectionObject($object);
$reflectionMethods = $reflectionObject->getMethods(\ReflectionMethod::IS_PUBLIC);
$attributes = array();
foreach ($reflectionMethods as $method) {
if ($this->isGetMethod($method)) {
$attributeName = strtolower(substr($method->name, 3));
$attributeValue = $method->invoke($object);
if (null !== $attributeValue && !is_scalar($attributeValue) && LimitedRecursiveGetSetMethodNormalizer::$limit>0) {
LimitedRecursiveGetSetMethodNormalizer::$limit--;
$attributeValue = $this->serializer->normalize($attributeValue, $format);
LimitedRecursiveGetSetMethodNormalizer::$limit++;
}
$attributes[$attributeName] = $attributeValue;
}
}
return $attributes;
}
/**
* Checks if a method's name is get.* and can be called without parameters.
*
* @param ReflectionMethod $method the method to check
* @return Boolean whether the method is a getter.
*/
private function isGetMethod(\ReflectionMethod $method)
{
return (
0 === strpos($method->name, 'get') &&
3 < strlen($method->name) &&
0 === $method->getNumberOfRequiredParameters()
);
}
}
And usage
LimitedRecursiveGetSetMethodNormalizer::$limit=3;
$serializer = new Serializer(array(new LimitedRecursiveGetSetMethodNormalizer()), array('json' => new
JsonEncoder()));
$response =new Response($serializer->serialize($YOUR_OBJECT,'json'));
这篇关于Doctrine实体到Json使用GetSetMethodNormalizer返回致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!