/** * @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(); }
/** * 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(); }