我遇到了一个关于ORM\PrePersist的问题,接下来是我的Employee实体和EmployeeController:
<?php
namespace NlcInformationBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyComponentHttpFoundationFileUploadedFile;
/**
* Employee
* @ORMEntity()
* @ORMHasLifecycleCallbacks()
*/
class Employee
{
...
/**
* @ORMPrePersist
* @ORMPreUpdate
*/
public function preUpload()
{
dump(1);die;
}
...
}
<?php
namespace NlcInformationBundleController;
use NlcInformationBundleEntityEmployee;
use SymfonyBundleFrameworkBundleControllerController;
use SensioBundleFrameworkExtraBundleConfigurationMethod;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SymfonyComponentHttpFoundationRequest;
/**
* 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('NlcInformationBundleFormEmployeeType', $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(),
));
}
并收到此消息:
使用参数 ["\u9093\u5b66\u6587", 4, "\u7537", "\u672c\u79d1", null, null, "2013-01-01 00:00:00", "2013-01-01 00:00:00", "2013-01-01 00:00:00", 1] 执行"插入员工(姓名、年龄、性别、教育、照片、jl、created_at、updated_at、category_id(值 (?, ?, ?, ?, ?, ?, ?, ?, ?("时发生异常:
SQLSTATE[23000]:完整性约束冲突:1048 列"照片"不能为空
嗨,您必须在列的设置器中传递预持久 确实想应用
比如像这样
/**
* @ORMPrePersist
*/
public function setPhotoAtValue()
{
$this->createdAt = new DateTime();
}