dty63504 2015-04-21 14:53
浏览 48
已采纳

Symfony2 @Assert \有效:遍历Vs深

I have a simple form that takes a very long time :

$builder->add('manufacturer', 'entity', array(
    'class' => 'XBundle\Entity\Manufacturer',
    ....

))
->add('type','entity', array(
  'class'    => 'XBundle\Entity\Entity\Type',


))
->add('size','entity', array(
  'class'    => 'XBundle\Entity\Size',


))
->add('serial', 'text', array(

    'required'=>true,
  ))
;

After installing xhproof and investigating the problem I found out that the validation is taking a big amount of time.

/**
* @ORM\ManyToOne(targetEntity="Ttm\HardwareBundle\Entity\Manufacturer", inversedBy="models")
* @Assert\Valid() <--- this line is the problem
*/
private $manufacturer;

Symfony2's documentation about valid annotation is not very clear:

traverse

type: boolean default: true

If this constraint is applied to a property that holds an array of objects, then each object in that array will be validated only if this option is set to true.

deep

type: boolean default: false

If this constraint is applied to a property that holds an array of objects, then each object in that array will be validated recursively if this option is set to true.

Source: http://symfony.com/doc/current/reference/constraints/Valid.html

My question is, What's the difference between the two options and which values guarantee to me a better performance ?

  • 写回答

1条回答 默认 最新

  • douzhi3776 2015-04-21 20:15
    关注

    http://api.symfony.com/2.4/Symfony/Component/Validator/ExecutionContextInterface.html

    if you leave it default, by using traverse link, it will cover all objects inside the nested array, ignoring any arrays of objects that it sees inside this parent object, i.e. will skip validation on them. It also has to look for, and cover anything that is instance of \Traversable

    using deep link validates into these objects looking for a nested collection. It might even skip validation on those that dont meet that criteria, like kind of a filter so be careful with that.

    Otherwise, if your seeing a difference in performance either the traverse may be poorly programmed out or having to look for instances of Traversable is hard hitting.

    If your not needing traversable, for your case you might consider using only deep since its faster, but make sure your results arent clipped.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题