I had group of objects which extends abstract class. I validate it in models. When I insert it one by one its everything ok, but now i wanna add possibility to import it from files. I had all logic to it but I had problem with validation because each type of object has different validation options. For example object A has properties name, type, speed max, speed min, intenstity another object B has properties name, type. In each model I had Assertions, but some of them base on that what is in database. For example if object in database had type: Movement, name: Running and intensity: "Fast" each new object with the same type and name cannot had the same intensity. The problem is that if I try insert few rows in one time each of model itself pass validate with db entities but in array of models can be 2, 3 or maybe more new models which has the same type, name and intensity. If persist and flush them one by one it works fine but if I wanna persist it by group like that:
$batchSize = 10;
$i = 1;
foreach ($activities as $activity) {
$entityManager->persist($activity);
if (($i % $batchSize) === 0) {
$entityManager->flush();
$entityManager->clear();
}
++$i;
}
$entityManager->flush();
It can be group of the same objects which will be inserted to db. I know the problem is simple I validate in model by searching in db another object with the same properties, but persisted object it's not in db yet so each next in group not flushed objects can pass validation before insert to db. I think about few possibilities to fix that problem: First one is push models to db one by one but it's can be performance issue when user wanna insert really big piece of data in one file. Second one is create ArrayCollection of models and try validate it by Collection Assert and this sounds best to me. Third option is create validator service for arrays of models and there get values of properties and check just values from models in array.
But maybe it is another option to resolve that problem?? Thanks a lot for any ideas :)