Bolt\Storage\Field\Collection\RepeatingFieldCollection::update PHP Метод

update() публичный Метод

public update ( RepeatingFieldCollection $collection ) : RepeatingFieldCollection[]
$collection RepeatingFieldCollection
Результат RepeatingFieldCollection[]
    public function update(RepeatingFieldCollection $collection)
    {
        $updated = [];
        // First give priority to already existing entities
        foreach ($collection->flatten() as $entity) {
            $master = $this->getOriginal($entity);
            $master->setValue($entity->getValue());
            $master->setFieldtype($entity->getFieldtype());
            $master->handleStorage($this->getFieldType($entity->getFieldname()));
            $updated[] = $master;
        }
        $deleted = [];
        foreach ($this->flatten() as $old) {
            if (!in_array($old, $updated)) {
                $deleted[] = $old;
            }
        }
        // Clear the collection so that we re-add only the updated elements
        $this->clear();
        foreach ($updated as $new) {
            $this->add($new);
        }
        return $deleted;
    }

Usage Example

Пример #1
0
 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);
         }
         foreach ($toDelete as $entity) {
             $repo->delete($entity);
         }
     });
 }