0
votes

I got a problem about ORM\PrePersist,the next is my Employee entity and 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(),
    ));
}

and got this message:

An exception occurred while executing 'INSERT INTO employee (name, age, sex, education, photo, jl, created_at, updated_at, category_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["\u9093\u5b66\u6587", 4, "\u7537", "\u672c\u79d1", null, null, "2013-01-01 00:00:00", "2013-01-01 00:00:00", 1]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'photo' cannot be null

1
as you can see from the error message $employee must have a photo set which isn't the case - so it's not about prepersist so far. please fix this issue first and check whether you still face one.LBA
oh, thanks, my question is not clear indeed, and I have change it here stackoverflow.com/questions/52306113/…Dave Deng

1 Answers

0
votes

Hi you must pass PrePersist in setter for column did want to apply

For example Like this

/**
 * @ORM\PrePersist
 */
public function setPhotoAtValue()
{
    $this->createdAt = new \DateTime();
}