为一个实体阻止 EntityManager 而不是另一个实体

Prevent EntityManager for one entity but not the other

所以我有一个控制器,它基本上通过向管理员发送电子邮件来提交对类别的编辑以供批准。在我决定添加操作 table 来存储操作历史记录(例如类别:编辑)之前,这很好。

出现的问题是,通过使用 entityManager 向操作添加数据 table,由于事件观察器,它会自动更新类别实体。

我尝试了 google,但找不到关于将 entityManager 设置为仅一个实体的任何内容。

这是我当前的控制器:

<?php
    namespace App\Controller\Category\Edit;

    use App\Entity\Action;
    use App\Entity\Category;
    use App\Entity\User;
    use App\Form\Category\EditCategoryType;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;

    class EditController extends Controller
    {
        public function edit($id, Request $request, \Swift_Mailer $mailer)
        {
            $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');

            $category     = $this->getDoctrine()->getRepository(Category::class)->find($id);
            $categoryGuru = $category->getGuru();
            $guruName     = $categoryGuru->getUsername();

            $category->setGuru($categoryGuru);

            $form = $this->createForm(EditCategoryType::class, $category);
            $form->handleRequest($request);

            if ($form->isSubmitted() && $form->isValid()) {
                $formData = $form->getData();

                $name        = $formData->getName();
                $description = $formData->getDescription();

                $newGuruId = $form['guru']->getData();
                $newGuru   = $this->getDoctrine()->getRepository(User::class)->find($newGuruId);

                $actionId = $this->setAction();

                $approveUrl  = $category->getId();
                $approveUrl .= '/'. $actionId;
                $approveUrl .= '/'. $name;
                $approveUrl .= '/'. $description;
                $approveUrl .= '/'. $newGuru->getId();

                $message = (new \Swift_Message('Category Edit Request - '. $category->getName()))
                    ->setFrom('some@email.com')
                    ->setTo('another@email.co.uk')
                    ->setBody(
                        $this->renderView(
                            'emails/category/edit-request.html.twig',
                            array(
                                'category' => $category->getName(),
                                'category_new_name' => $name,
                                'description' => $category->getDescription(),
                                'category_new_description' => $description,
                                'guru' => $guruName,
                                'category_new_guru' => $newGuru->getUsername(),
                                'category_new_guru_id' => $newGuru->getId(),
                                'category_id' => $category->getId(),
                                'category_author' => $this->getUser()->getUsername(),
                                'approve_url' => $approveUrl,
                                'action_id' => $actionId
                            )
                        ),
                        'text/html'
                    );

                $mailer->send($message);

                $this->addFlash('success', 'Category Edit Submitted for Review.');

                return $this->redirectToRoute('category_list');
            }

            return $this->render(
                'category/edit.html.twig',
                array('form' => $form->createView(), 'category' => $category, 'guru' => $categoryGuru)
            );
        }

        # this was originally a part of the above controller
        # tried separating to see if it would work - didn't
        public function setAction()
        {
            $action = new Action();

            $entityManager = $this->getDoctrine()->getManager();

            # set action data
            $action->setDate(new \DateTime());
            $action->setUserId($this->getUser()->getId());
            $action->setDescription($this->getUser()->getUsername(). ' has edited a category');
            $action->setStatus('pending');
            $action->setType('Category: edit');

            $entityManager->persist($action);
            $entityManager->flush();

            return $action->getId();
        }
    }

我得出的答案是,不要。使用存储库 class:

CategoryRepository.php

public function addAction($userId, $description, $status, $type)
{
    $entityManager = $this->getEntityManager();
    $connection    = $entityManager->getConnection();

    $date = date('Y-m-d H:i:s', strtotime('now'));

    $connection->insert(
        'action',
        array(
            'type' => $type,
            'user_id' => $userId,
            'date' => $date,
            'description' => $description,
            'status' => $status
        )
    );

    return $entityManager->getConnection()->lastInsertId();
}

然后在控制器中调用它 - 不再 classing entityManager 问题。