symfony - 防止 EntityManager 为一个实体而不是另一个实体
问题描述
所以我有一个控制器,它基本上通过向管理员发送电子邮件来提交类别的编辑以供批准。在我决定添加一个动作表来存储动作历史(例如类别:编辑)之前,这很好。
出现的问题是,通过使用 entityManager 将数据添加到操作表中,由于事件观察程序,它会自动更新类别实体。
我尝试了谷歌,但找不到任何关于将 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();
}
}
解决方案
我得到的答案是,不要。使用存储库类:
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();
}
然后在控制器中调用它——不再对 entityManager 问题进行分类。
推荐阅读
- couchdb - 更新设计文档时,CouchDB 是否会无条件地重建索引?
- python - 正则表达式:提取 [..] 之间的字符串
- node.js - 当一组用户完成一项任务时,如何执行一项任务?这可以使用 nodejs 和 mongodb 来完成吗?
- python - alembic.util.exc.CommandError:找不到由“f1942fde9843”标识的修订版
- python - 为什么scrapy 说我正在迭代一个“itemMeta”对象?
- r - R中的共现矩阵,对变量的值求和以填充单元格
- regex - ingress rewrite-target 中编号占位符的含义是什么?
- c# - 编写 ASP.NET 自定义存储提供程序
- python - 如何将个人模块导入气流
- angular - Angular 10 - 如何在集合更改时自动加载新项目