我有实体项目并且使用Timestampable,当我创建实体时,我在创建时有日期时间,更新时我有更新时的日期时间但是当我删除时我在数据库中删除了我的实体但是我没有字段deletedAt。我不想要删除实体我只想设置已删除或如何使用从Timestampable删除
/**
* Project
*
* @ORM\Table(name="project")
* @ORM\Entity(repositoryClass="Artel\ProfileBundle\Entity\Repository\ProjectRepository")
* @ExclusionPolicy("all")
*/
class Project
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @Type("integer")
*/
private $id;
/**
* @Gedmo\Slug(fields={"id", "level", "main_skill", "name"}, separator="-")
* @ORM\Column(name="slug", type="string", length=255, nullable=false)
* @Assert\Length(min=3, max=255)
* @Expose()
* @Type("string")
*/
protected $slug;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
* @Assert\NotBlank
* @Expose()
* @Type("string")
*/
private $description;
/**
* @var datetime $created
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
* @Expose()
* @Type("DateTime")
*/
private $created;
/**
* @var \DateTime
*
* @ORM\Column(name="deleted_at", type="datetime", nullable=true)
*/
protected $deletedAt;
/**
* @var \DateTime
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(name="updated_at", type="datetime")
*/
protected $updatedAt;
这是我的行动
/**
* Deletes a Project entity.
*
* @Route("/{id}", name="admin_project_delete")
* @Method("DELETE")
* @ParamConverter("entity", class="ArtelProfileBundle:Project")
*/
public function deleteAction(Request $request, $entity)
{
$form = $this->createDeleteForm($entity->getId());
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('admin_all_project'));
}
private function createDeleteForm($id)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('admin_project_delete', array('id' => $id)))
->setMethod('DELETE')
->add('submit', 'submit', array('label' => 'Delete'))
->getForm()
;
}
答案 0 :(得分:1)
由于您已经在使用{{1}},请查看Gedmo SoftDeleteable behaviour。此行为不仅可以避免删除您的实体,还可以将删除时间戳存储在字段中,就像您请求的那样。
答案 1 :(得分:0)
我解决了我的任务:
stof_doctrine_extensions:
orm:
default:
...
softdeleteable: true
doctrine:
...
orm:
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
并添加到etity @Gedmo \ SoftDeleteable(fieldName =" deletedAt")
/**
* Project
*
* @ORM\Table(name="project")
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
* @ORM\Entity(repositoryClass="Artel\ProfileBundle\Entity\Repository\ProjectRepository")
* @ExclusionPolicy("all")
*/
class Project
{
/////
/**
* @var \DateTime $deletedAt
*
* @ORM\Column(name="deleted_at", type="datetime", nullable=true)
*/
protected $deletedAt;
}
和行动
/**
* Deletes a Project entity.
*
* @Route("/{id}", name="admin_project_delete")
* @Method("DELETE")
* @ParamConverter("entity", class="ArtelProfileBundle:Project")
*/
public function deleteAction(Request $request, $entity)
{
$form = $this->createDeleteForm($entity->getId());
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('admin_all_project'));
}
然后我在我的数据库中有实体并设置了deletedAt datetime