php - symfony 2.8 @ORM\PrePersist 不工作
问题描述
我遇到了关于 ORM\PrePersist 的问题,接下来是我的 Employee 实体和 EmployeeController:
<?php
namespace Nlc\InformationBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Employee
* @ORM\Entity()
* @ORM\HasLifecycleCallbacks()
*/
class Employee
{
...
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function preUpload()
{
dump(1);die;
}
...
}
<?php
namespace Nlc\InformationBundle\Controller;
use Nlc\InformationBundle\Entity\Employee;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
/**
* Employee controller.
*
* @Route("nlc_employee")
*/
class EmployeeController extends Controller
{
/**
* Creates a new employee entity.
*
* @Route("/new", name="nlc_employee_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$employee = new Employee();
$form = $this->createForm('Nlc\InformationBundle\Form\EmployeeType', $employee);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($employee);
$em->flush();
return $this->redirectToRoute('nlc_employee_show', array('id' => $employee->getId()));
}
return $this->render('employee/new.html.twig', array(
'employee' => $employee,
'form' => $form->createView(),
));
}
并收到此消息:
执行'INSERT INTO employee (name, age, sex, education, photo, jl, created_at, updated_at, category_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)' 时发生异常参数 ["\u9093\u5b66\u6587", 4, "\u7537", "\u672c\u79d1", null, null, "2013-01-01 00:00:00", "2013-01-01 00: 00:00”,1]:
SQLSTATE [23000]:违反完整性约束:1048 列“照片”不能为空
解决方案
嗨,您必须在 setter 中通过 PrePersist 才能获得想要申请的列
例如像这样
/**
* @ORM\PrePersist
*/
public function setPhotoAtValue()
{
$this->createdAt = new \DateTime();
}
推荐阅读
- ios - 如何在我的 iOS 应用程序中处理暗模式的黑色内容图像?
- r - 成对组合分组的 R 数据帧的行并将函数应用于每一对
- python - 浮点数需要显示为分数
- django - 如何通过 Django 中的表单数据发布将文件上传到 GAE?
- office-js - Word Offic.js 加载项运行另一个 Office.js
- javascript - 在引导可排序元素内映射 - 停止拖动
- javascript - jsPDF在fromHtml方法中更改文本的字体
- python - SQLAlchemy 模糊外键
- python - 来自 select 的 sqlalchemy 插入正在丢失列的正确类型
- javascript - 使用 ng2-charts,如何“缩小”?