/** * Append record inserts to the query. * * @param QuerySet $queries * @param mixed $entity * @param array $toInsert */ protected function appendInsertQueries(QuerySet $queries, $entity, array $toInsert) { foreach ($toInsert as $item) { $item = (string) $item; $ins = $this->em->createQueryBuilder()->insert($this->mapping['target'])->values(['content_id' => ':content_id', 'contenttype' => ':contenttype', 'taxonomytype' => ':taxonomytype', 'slug' => ':slug', 'name' => ':name'])->setParameters(['content_id' => $entity->id, 'contenttype' => $entity->getContenttype(), 'taxonomytype' => $this->mapping['fieldname'], 'slug' => Slugify::create()->slugify($item), 'name' => isset($this->mapping['data']['options'][$item]) ? $this->mapping['data']['options'][$item] : $item]); $queries->onResult(function ($query, $result, $id) use($ins) { if ($query === $ins && $result === 1 && $id) { $query->setParameter('content_id', $id); } }); $queries->append($ins); } }
/** * Query to insert new field values. * * @param QuerySet $queries * @param array $changes * @param $entity */ protected function addToUpdateQuery(QuerySet $queries, $changes, $entity) { foreach ($changes as $fieldValue) { $repo = $this->em->getRepository(get_class($fieldValue)); $field = $this->getFieldType($fieldValue->getFieldname()); $type = $field->getStorageType(); $typeCol = 'value_' . $type->getName(); $fieldValue->{$typeCol} = $fieldValue->getValue(); // This takes care of instances where an entity might be inserted, and thus not // have an id. This registers a callback to set the id parameter when available. $queries->onResult(function ($query, $result, $id) use($repo, $fieldValue) { if ($result === 1) { $repo->save($fieldValue); } }); } }
/** * {@inheritdoc} */ 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); $collection->filterInverseValues($existingInverse); $repo = $this->em->getRepository('Bolt\\Storage\\Entity\\Relations'); // 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) { foreach ($collection as $entity) { $entity->from_id = $id; $repo->save($entity); } foreach ($toDelete as $entity) { $repo->delete($entity); } }); }
/** * {@inheritdoc} */ public function persist(QuerySet $queries, $entity) { $field = $this->mapping['fieldname']; $taxonomy = $entity->getTaxonomy()->getField($field); // Fetch existing taxonomies $existingDB = $this->getExistingTaxonomies($entity) ?: []; $collection = $this->em->getCollectionManager()->create('Bolt\\Storage\\Entity\\Taxonomy'); $collection->setFromDatabaseValues($existingDB); $toDelete = $collection->update($taxonomy); $repo = $this->em->getRepository('Bolt\\Storage\\Entity\\Taxonomy'); $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); } }); }