Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 @Assert\Valid: traverse Vs deep

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 ?

like image 682
zizoujab Avatar asked Apr 21 '15 14:04

zizoujab


1 Answers

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.

like image 84
blamb Avatar answered Nov 14 '22 22:11

blamb