Bolt\Storage\Field\Type\RelationType::persist PHP Метод

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

public persist ( Bolt\Storage\QuerySet $queries, $entity )
$queries Bolt\Storage\QuerySet
    public function persist(QuerySet $queries, $entity)
    {
        $field = $this->mapping['fieldname'];
        $relations = $entity->getRelation()->getField($field);
        // Fetch existing relations and create two sets of records, updates and deletes.
        $existingDB = $this->getExistingRelations($entity) ?: [];
        $existingInverse = $this->getInverseRelations($entity) ?: [];
        $collection = $this->em->createCollection('Bolt\\Storage\\Entity\\Relations');
        $collection->setFromDatabaseValues($existingDB);
        $toDelete = $collection->update($relations);
        $repo = $this->em->getRepository('Bolt\\Storage\\Entity\\Relations');
        // If we have bidirectional relations we need to delete the old inverted relations
        $inverseCollection = $this->em->createCollection('Bolt\\Storage\\Entity\\Relations');
        $inverseCollection->setFromDatabaseValues($existingInverse);
        // Add a listener to the main query save that sets the from ID on save and then saves the relations
        $queries->onResult(function ($query, $result, $id) use($repo, $collection, $toDelete, $inverseCollection) {
            foreach ($collection as $entity) {
                $entity->from_id = $id;
                $repo->save($entity, $silenceEvents = true);
            }
            foreach ($inverseCollection as $entity) {
                $repo->delete($entity);
            }
            foreach ($toDelete as $entity) {
                $repo->delete($entity);
            }
        });
    }