I'm using SoftDeletable trait in entities from https://github.com/KnpLabs/DoctrineBehaviors/#softDeletable It's working fine, but sometimes I'd like to force delete the entity. How can I do that?
When I use $em->remove($entity), it gets soft-deleted but I need to remove it completely from the database.
I found simple solution. Entity will be softdeleted at first, but if it is already been soft deleted it will be hard deleted so my simple solution was:
$entity->setDeletedAt(new DateTime());
$entityManager->remove($entity);
$entityManager->flush();
Of course you need to disable 'softdelete' filter first and deletedAt is a sofdelete field.
Just remove the subscriber from the EventManager and add it back after the remove() / flush() operation.
// get the event-manager
$eventManager = $this->get('doctrine')->getEventManager();
// get the listener
$subscriber = $this->get('knp.doctrine_behaviors.softdeletable_subscriber');
// remove the the subscriber for all events
$eventManager->removeEventListener($subscriber->getSubscribedEvents(), $subscriber);
// remove the entity
$em->remove($entity);
$em->flush();
// add it back to the event-manager
$eventManager->addEventSubscriber($subscriber);
Since nifr's answer doesn't work anymore in the current version of the behaviors, I had a deeper look at the problem and got to that solution:
$em = $this->getDoctrine()->getManager();
// initiate an array for the removed listeners
$originalEventListeners = array();
// cycle through all registered event listeners
foreach ($em->getEventManager()->getListeners() as $eventName => $listeners) {
foreach ($listeners as $listener) {
if ($listener instanceof Knp\DoctrineBehaviors\ORM\SoftDeletable\SoftDeletableSubscriber) {
// store the event listener, that gets removed
$originalEventListeners[$eventName] = $listener;
// remove the SoftDeletableSubscriber event listener
$em->getEventManager()->removeEventListener($eventName, $listener);
}
}
}
// remove the entity
$em->remove($entity);
$em->flush();
// re-add the removed listener back to the event-manager
foreach ($originalEventListeners as $eventName => $listener) {
$em->getEventManager()->addEventListener($eventName, $listener);
}
See also https://stackoverflow.com/a/22838467/2564552
I have writte a service to disable and reenable the soft delete filter behaviour:
<?php
namespace App\Util;
use Doctrine\ORM\EntityManagerInterface;
use Gedmo\SoftDeleteable\SoftDeleteableListener;
class SoftDeleteFilter
{
/**
* #var string
*/
private $eventName;
/**
* #var object
*/
private $originalEventListener;
/**
* #param EntityManagerInterface $em
*/
public function removeSoftDeleteFilter(EntityManagerInterface $em)
{
foreach ($em->getEventManager()->getListeners() as $eventName => $listeners) {
foreach ($listeners as $listener) {
if ($listener instanceof SoftDeleteableListener) {
if ($eventName === 'onFlush') {
$this->eventName = $eventName;
$this->originalEventListener = $listener;
$em->getEventManager()->removeEventListener($eventName, $listener);
}
}
}
}
}
/**
* #param EntityManagerInterface $em
*/
public function undoRemoveSoftDeleteFilter(EntityManagerInterface $em)
{
if (empty($this->originalEventListener) || empty($this->eventName)) {
throw new \Exception('can not undo remove, soft delete listener was not removed');
}
// re-add the removed listener back to the event-manager
$em->getEventManager()->addEventListener($this->eventName, $this->originalEventListener);
}
}
usage:
$this->softDeleteFilter->removeSoftDeleteFilter($this->entityManager);
$this->entityManager->remove($issue);
$this->entityManager->flush();
$this->softDeleteFilter->undoRemoveSoftDeleteFilter($this->entityManager);
Related
I have implemented an EventListener class and declare it in services.yaml
I'd like to return to my Controller a variable when entity is persited and send this variable to twig template. I want to show a step form in my view showing Entity name in green for example when data has been persisted in database. If it works I will use the same process in another controller where I persist multiple entities. To sum up: How to notify a controller that a specific entity has been persisted by passing a variable?
The eventlistener
<?php
namespace App\EventListener;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use App\Entity\Article;
class TodoListener {
public function postPersist(LifecycleEventArgs $args) {
$entity = $args->getObject();
if(!$entity instanceof Article)
return;
$var = 'foo';
return $var;
}
}
services.yaml
App\EventListener\TodoListener:
tags:
- { name: doctrine.event_listener, event: postPersist }
Controller
/**
* #Route("/blog/new", name="blog_create")
* #Route("/blog/{id}/edit", name="blog_edit")
*/
public function form(Article $article = null, Request $request, ObjectManager $manager)
{
if (!$article) {
$article = new Article();
}
$form = $this->createForm(ArticleType::class, $article);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if (!$article->getId()) {
$article->setCreatedAt(new \dateTime());
}
$manager->persist($article);
$manager->flush();
/**
* Get back variable when entity is persisted ???
*/
return $this->redirectToRoute('blog_show', ['id' => $article->getId()]);
}
return $this->render('blog/create.html.twig', [
'formArticle' => $form->createView(),
'editMode' => $article->getId() !== null
]);
}
In short: you can’t.
You can try to work around it with a custom symfony event, but is very bad.
If you want to know if an entity is new or already persisted you should call getEntityState on entity manager’s UnitOfWork or split the flows between actions (write two distinct actions for new and edit).
Anyway, just a suggestion: set the createdAt field into the entity constructor ;)
I have the following issue with my form class that extends the ContentEntityForm class.
When calling the parent buildForm which is needed my system runs out of memory.
/**
* {#inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
// Here is already runs of memory. $form is never initiated.
/* #var $entity \Drupal\sg_configuration_rule\Entity\ConfigurationRule */
$entity = $this->entity;
$form_state->set('old_cron_value', $entity->get('cron_settings')->first()->value);
$type = FALSE;
if (!$entity->isNew()) {
$type = $entity->getPluginInstance()->getPluginId();
}
if ($entity->isNew()) {
$type = \Drupal::request()->query->get('type');
if (!$type) {
return new RedirectResponse(Url::fromRoute('configuration_rule.add_form_step1')->toString());
}
}
if ($type) {
try {
/** #var \Drupal\sg_base_api\Plugin\BaseApiPluginInterface $enabled_api */
$enabled_api = $this->baseApiPluginManager->createInstance($type);
}
catch (PluginException $exception) {
LoggerService::error($exception->getMessage());
return new RedirectResponse(Url::fromRoute('configuration_rule.add_form_step1')->toString());
}
$enabled_api->configRuleForm($form, $entity);
$form['plugin_type']['widget'][0]['value']['#value'] = $type;
$form['plugin_type']['widget'][0]['value']['#access'] = FALSE;
$form['plugin_type']['widget'][0]['value']['#disabled'] = TRUE;
$form['server_node']['widget']['#options'] = $this->getServerNodesByType($enabled_api->entityType());
}
$form['user_id']['#access'] = FALSE;
return $form;
}
When i check the parent function i noticed that the line:
$form = $this->form($form, $form_state); is causing this in the class EntityForm(Core method).
/**
* {#inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// During the initial form build, add this form object to the form state and
// allow for initial preparation before form building and processing.
if (!$form_state->has('entity_form_initialized')) {
$this->init($form_state);
}
// Ensure that edit forms have the correct cacheability metadata so they can
// be cached.
if (!$this->entity->isNew()) {
\Drupal::service('renderer')->addCacheableDependency($form, $this->entity);
}
// Retrieve the form array using the possibly updated entity in form state.
// This is causing my memory timeout.
$form = $this->form($form, $form_state);
// Retrieve and add the form actions array.
$actions = $this->actionsElement($form, $form_state);
if (!empty($actions)) {
$form['actions'] = $actions;
}
return $form;
}
If i comment that line out it is working fine but this is needed to save my values in config. Also this is core and should work.
Anyone else have this problem and knows the solutions to this?
Thanks.
This is solved, the error was that too much results where loaded in a select field.
I'm currently build an API with FosRestBundle. I search how can i make a reusable and optimize service for update my entities.
For now, in my controller i get two entites with paramConverter :
/**
* #Rest\Put("/enseignes/{enseigne_id}",
* requirements={"id" = "\d+"})
*#ParamConverter("enseigne", options={"id":"enseigne_id"})
*#ParamConverter("enseigneUpdate",
* converter="fos_rest.request_body")
*
* #Rest\View()
*/
public function updateEnseigneAction(Enseigne $enseigne,Enseigne $enseigneUpdate, ConstraintViolationList $violations)
{
if (count($violations)){
return $this->view($violations,Response::HTTP_BAD_REQUEST);
}
$mapData = $this->get('app.mapdata');
$mapData->mapDataOnEntity($enseigneUpdate,$enseigne);
$em = $this->getDoctrine()->getManager();
$em->flush();
return $enseigne;
}
So, one entity currently in my bdd with id parameter (and check if the id exist ), and another one who is validate with the converter of fosRest. GOOD
My service look like :
class MapData {
private $propertyMetadata;
private $serializer;
function __construct(MetadataFactory $metadataFactory,Serializer $serializer)
{
$this->propertyMetadata = $metadataFactory;
$this->serializer = $serializer;
}
public function mapDataOnEntity($entityUpdate, $targetEntity)
{
if (get_class($entityUpdate) != get_class($targetEntity)){
throw new BadRequestHttpException('Entity doesnt match');
}
$this->fillProperties($targetEntity,$entityUpdate );
}
/**
*
* #param object $targetEntity
* #param object $entityUpdate
*/
protected function fillProperties($targetEntity, $entityUpdate)
{
$propertyAccessor = new PropertyAccessor();
$reflectorTargetEntity = new \ReflectionClass($entityUpdate);
// dump($classAnnotaions);die();
// Recupere les propriétés de targetEntity
$properties = $reflectorTargetEntity->getProperties();
// dump($properties);die();
foreach ($properties as $property){
if ($property->getName() != 'id'){
$newValue = $propertyAccessor->getValue($entityUpdate,$property->getName());
$propertyAccessor->setValue($targetEntity,$property->getName(),$newValue);
}
}
}
}
First :
In the properties of my class i have the properties of relation with other entities and i don't want it !
Second:
How i can use this code for manage the PATCH too ? And what do you think about this solution ?
I've created my own service class and I have a function inside it, handleRedirect() that's supposed to perform some minimal logical check before choosing to which route to redirect.
class LoginService
{
private $CartTable;
private $SessionCustomer;
private $Customer;
public function __construct(Container $SessionCustomer, CartTable $CartTable, Customer $Customer)
{
$this->SessionCustomer = $SessionCustomer;
$this->CartTable = $CartTable;
$this->Customer = $Customer;
$this->prepareSession();
$this->setCartOwner();
$this->handleRedirect();
}
public function prepareSession()
{
// Store user's first name
$this->SessionCustomer->offsetSet('first_name', $this->Customer->first_name);
// Store user id
$this->SessionCustomer->offsetSet('customer_id', $this->Customer->customer_id);
}
public function handleRedirect()
{
// If redirected to log in, or if previous page visited before logging in is cart page:
// Redirect to shipping_info
// Else
// Redirect to /
}
public function setCartOwner()
{
// GET USER ID FROM SESSION
$customer_id = $this->SessionCustomer->offsetGet('customer_id');
// GET CART ID FROM SESSION
$cart_id = $this->SessionCustomer->offsetGet('cart_id');
// UPDATE
$this->CartTable->updateCartCustomerId($customer_id, $cart_id);
}
}
This service is invoked in the controller after a successful login or registration. I'm not sure what's the best way to access redirect()->toRoute(); from here (or if I should do it here).
Also if you have other comments on how my code is structured please feel free to leave them.
Using plugins within your services is a bad idea as they require a controller to be set. When a service is created and you inject a plugin it has no idea of the controller instance so it will result in an error exception. If you want to redirect the user you might just edit the response object as the redirect plugin does.
Notice that I stripped the code to keep the example clear and simple.
class LoginServiceFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new LoginService($container->get('Application')->getMvcEvent());
}
}
class LoginService
{
/**
* #var \Zend\Mvc\MvcEvent
*/
private $event;
/**
* RedirectService constructor.
* #param \Zend\Mvc\MvcEvent $event
*/
public function __construct(\Zend\Mvc\MvcEvent $event)
{
$this->event = $event;
}
/**
* #return Response|\Zend\Stdlib\ResponseInterface
*/
public function handleRedirect()
{
// conditions check
if (true) {
$url = $this->event->getRouter()->assemble([], ['name' => 'home']);
} else {
$url = $this->event->getRouter()->assemble([], ['name' => 'cart/shipping-info']);
}
/** #var \Zend\Http\Response $response */
$response = $this->event->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;
}
}
Now from within your controller you can do the following:
return $loginService->handleRedirect();
I have a problem with edititng embedded collection form. I have two object with many-to-one relation. When I create an object "Good" with related "photos" all successfully. When I update the Good object by adding some new photos all works fine too. But, if I try to delete a one photo in some Good object after update photo is not deleted.
Good.php
/**
* #ORM\OneToMany(targetEntity="Photo", mappedBy="good", cascade={"persist", "remove"})
**/
private $photos;
/**
* Add photos
*
* #param \VDKP\Site\BackendBundle\Entity\Photo $photos
* #return Good
*/
public function addPhoto(\VDKP\Site\BackendBundle\Entity\Photo $photos)
{
$photos->setGood($this);
$this->photos->add($photos);
return $this;
}
/**
* Remove photos
*
* #param \VDKP\Site\BackendBundle\Entity\Photo $photos
*/
public function removePhoto(\VDKP\Site\BackendBundle\Entity\Photo $photos)
{
$this->photos->removeElement($photos);
}
/**
* Get photos
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getPhotos()
{
return $this->photos;
}
Photo.php
/**
* #ORM\ManyToOne(targetEntity="Good", inversedBy="photos")
* #ORM\JoinColumn(name="good_id", referencedColumnName="id")
**/
private $good;
GoodController, updateACtion:
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('VDKPSiteBackendBundle:Good')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Good entity.');
}
$originalPhotos = new \Doctrine\Common\Collections\ArrayCollection();
foreach ($entity->getPhotos() as $photo) {
$originalPhotos->add($photo);
}
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
foreach ($originalPhotos as $photo) {
if (false === $entity->getPhotos()->contains($photo)) {
$photo->setGood(null);
$em->persist($photo);
}
}
$em->persist($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('good_edit', array('id' => $id)));
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
I did everything as written in the documentation here.
Sorry for my english. Thank you for your help.
It looks like you missed this part of docs:
foreach ($originalTags as $tag) {
if (false === $task->getTags()->contains($tag)) {
// remove the Task from the Tag
$tag->getTasks()->removeElement($task);
// if it was a many-to-one relationship, remove the relationship like this
// $tag->setTask(null);
$em->persist($tag);
// if you wanted to delete the Tag entirely, you can also do that
// $em->remove($tag);
}
}
So, I think you have to do something similar with your data types: Good and Photo.
I think, documentation is inaccurate, because:
In this part of code:
$originalTags = new ArrayCollection();
// Create an ArrayCollection of the current Tag objects in the database
foreach ($task->getTags() as $tag) {
$originalTags->add($tag);
}
we collect Tags, which have relations with current Task in database.
In this part of code:
foreach ($originalTags as $tag) {
if (false === $task->getTags()->contains($tag)) {
// remove the Task from the Tag
$tag->getTasks()->removeElement($task);
// if it was a many-to-one relationship, remove the relationship like this
// $tag->setTask(null);
$em->persist($tag);
// if you wanted to delete the Tag entirely, you can also do that
// $em->remove($tag);
}
}
we must compare $request data and $originalTags array data. But, we compare $originalTags with $task->getTags(), which is essentially the same.