/** * @inheritdoc */ public function persist(QuerySet $queries, $entity, EntityManager $em = null) { $field = $this->mapping['fieldname']; $target = $this->mapping['target']; $accessor = "get" . $field; $taxonomy = (array) $entity->{$accessor}(); // Fetch existing relations $existingQuery = $em->createQueryBuilder()->select('*')->from($target)->where('content_id = ?')->andWhere('contenttype = ?')->andWhere('taxonomytype = ?')->setParameter(0, $entity->id)->setParameter(1, $entity->getContenttype())->setParameter(2, $field); $result = $existingQuery->execute()->fetchAll(); $existing = array_map(function ($el) { return $el['slug']; }, $result); $proposed = $taxonomy; $toInsert = array_diff($proposed, $existing); $toDelete = array_diff($existing, $proposed); foreach ($toInsert as $item) { $ins = $em->createQueryBuilder()->insert($target); $ins->values(['content_id' => '?', 'contenttype' => '?', 'taxonomytype' => '?', 'slug' => '?', 'name' => '?'])->setParameters([0 => $entity->id, 1 => $entity->getContenttype(), 2 => $field, 3 => $item, 4 => $this->mapping['data']['options'][$item]]); $queries->append($ins); } foreach ($toDelete as $item) { $del = $em->createQueryBuilder()->delete($target); $del->where('content_id=?')->andWhere('contenttype=?')->andWhere('taxonomytype=?')->andWhere('slug=?')->setParameters([0 => $entity->id, 1 => $entity->getContenttype(), 2 => $field, 3 => $item]); $queries->append($del); } }
/** * Append record deletes to the query. * * @param QuerySet $queries * @param mixed $entity * @param array $toDelete */ protected function appendDeleteQueries(QuerySet $queries, $entity, array $toDelete) { foreach ($toDelete as $item) { $del = $this->em->createQueryBuilder()->delete($this->mapping['target'])->where('content_id = :content_id')->andWhere('contenttype = :contenttype')->andWhere('taxonomytype = :taxonomytype')->andWhere('slug = :slug')->setParameters(['content_id' => $entity->id, 'contenttype' => $entity->getContenttype(), 'taxonomytype' => $this->mapping['fieldname'], 'slug' => $item]); $queries->append($del); } }
/** * {@inheritdoc} */ public function update($entity, $exclusions = []) { $querySet = new QuerySet(); $querySet->setParentId($entity->getGuid()); $qb = $this->em->createQueryBuilder(); $qb->update($this->getTableName())->where('guid = :guid')->setParameter('guid', $entity->getGuid()); $querySet->append($qb); /** @var \Bolt\Storage\Entity\Entity $entity */ $this->persist($querySet, $entity, ['id'] + $exclusions); return $querySet->execute(); }
public function persist(QuerySet $queries, $entity, EntityManager $em = null) { $key = $this->mapping['fieldname']; $qb = $queries->getPrimary(); $value = $entity->get($key); if (!$value instanceof Url) { $value = Url::fromNative($value); } $qb->setValue($key, ':' . $key); $qb->set($key, ':' . $key); $qb->setParameter($key, (string) $value); }
/** * 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); } }); }
/** * Updates an object into the database. * * @param object $entity The entity to update. * @param string[] $exclusions Ignore updates to these fields * * @return bool */ public function update($entity, $exclusions = []) { $querySet = new QuerySet(); $querySet->setParentId($entity->getId()); $qb = $this->em->createQueryBuilder(); $qb->update($this->getTableName())->where('id = :id')->setParameter('id', $entity->getId()); $querySet->append($qb); $this->persist($querySet, $entity, $exclusions); return $querySet->execute(); }
/** * {@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); } }); }