public function persist(QuerySet $queries, $entity)
{
$this->normalize($entity);
$key = $this->mapping['fieldname'];
$accessor = 'get' . ucfirst($key);
$proposed = $entity->{$accessor}();
$collection = new RepeatingFieldCollection($this->em, $this->mapping);
$existingFields = $this->getExistingFields($entity) ?: [];
foreach ($existingFields as $group => $ids) {
$collection->addFromReferences($ids, $group);
}
$toDelete = $collection->update($proposed);
$repo = $this->em->getRepository('Bolt\\Storage\\Entity\\FieldValue');
$queries->onResult(function ($query, $result, $id) use($repo, $collection, $toDelete) {
foreach ($collection as $entity) {
$entity->content_id = $id;
$repo->save($entity, $silenceEvents = true);
}
foreach ($toDelete as $entity) {
$repo->delete($entity);
}
});
}