SQLSTATE [23000]:违反完整性约束:1048列“ user_id”不能为空

时间:2018-10-11 16:26:22

标签: symfony

我正在使用symfony 3.4。

我有一个链接到实体campagne(campaign)的实体用户(一对多),当我想添加新的campagne时会显示此错误:

使用参数[“ ycamp2”,null]执行'INSERT INTO compagne(desc,user_id)VALUES(?,?)'时发生异常:

SQLSTATE [23000]:违反完整性约束:1048列'user_id'不能为空

坎帕涅类:

class Campagne
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
public $id;

/**
 * @var string|null
 *
 * @ORM\Column(name="desc", type="string", length=255, nullable=true, unique=true)
 *
 */
private $desc;


/**
* @ORM\ManyToOne(targetEntity="XxBundle\Entity\User", inversedBy="campagnes") 
* @ORM\JoinColumn(nullable=false)
*/
private $user;

//...
/**
 * Set user.
 *
 * @param \XxBundle\Entity\User $user
 *
 * @return Campagne
 */
public function setUser(\XxBundle\Entity\User $user)
{
    $this->user = $user;

    return $this;
}

/**
 * Get user.
 *
 * @return \XxBundle\Entity\User
 */
public function getUser()
{
    return $this->user;
}
}

班级用户:

 class User extends BaseUser
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

//...

/**
 * @ORM\OneToMany(targetEntity="XxBundle\Entity\Campagne", mappedBy="user", cascade={"persist", "remove"})
 */    
private $campagnes; 




//    .................CAMPAGNE..............

/**
 * Add campagne.
 *
 * @param \XxBundle\Entity\Capagne $campagne
 *
 * @return User
 */
public function addCampagne(\XxBundle\Entity\Campagne $campagne)
{

    $this->campagnes[] = $campagne;

    return $this;
}

/**
 * Remove campagne.
 *
 * @param \XxBundle\Entity\Campagne $campagne
 *
 * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
 */
public function removeCampagne(\XxBundle\Entity\Campagne $campagne)
{
    return $this->campagnes->removeElement($campagne);
}

/**
 * Get campagnes.
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getCampagnes()
{
    return $this->campagnes;
}

}

campagne控制器:

class CampagneController extends Controller
 {
/**
 * Lists all campagne entities.
 *
 */
public function indexAction()
{
    $em = $this->getDoctrine()->getManager();

    $campagnes= $em->getRepository('XxBundle:Campagne')->findAll();

    return $this->render('campagne/index.html.twig', array(
        'campagnes' => $campagnes,
    ));
}

/**
 * Creates a new campagne entity.
 *
 */
public function newAction(Request $request)
{
    $campagne = new Campagne();
    $form = $this->createForm('XxBundle\Form\CampagneType', $campagne);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($user);
        $em->flush();

        return $this->redirectToRoute('campagne_show', array('id' => $campagne->getId()));
    }

    return $this->render('campagne/new.html.twig', array(
        'campagne' => $campagne,
        'form' => $form->createView(),
    ));
}

/**
 * Finds and displays a campagne entity.
 *
 */
public function showAction(Campagne $campagne)
{
    $deleteForm = $this->createDeleteForm($campagne);

    return $this->render('campagne/show.html.twig', array(
        'campagne' => $campagne,
        'delete_form' => $deleteForm->createView(),
    ));
}

/**
 * Displays a form to edit an existing campagne entity.
 *
 */
public function editAction(Request $request, Campagne $campagne)
{
    $deleteForm = $this->createDeleteForm($campagne);
    $editForm = $this->createForm('XxBundle\Form\CampagneType', $Campagne);
    $editForm->handleRequest($request);

    if ($editForm->isSubmitted() && $editForm->isValid()) {
        $this->getDoctrine()->getManager()->flush();

        return $this->redirectToRoute('campagne_edit', array('id' => $campagne->getId()));
    }

    return $this->render('Campagne/edit.html.twig', array(
        'campagne' => $campagne,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}

/**
 * Deletes a campagne entity.
 *
 */
public function deleteAction(Request $request, Campagne $campagne)
{
    $form = $this->createDeleteForm($campagne);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->remove($campagne);
        $em->flush();
    }

    return $this->redirectToRoute('campagne_index');
}

/**
 * Creates a form to delete a campagne entity.
 *
 * @param Campagne $campagne The campagne entity
 *
 * @return \Symfony\Component\Form\Form The form
 */
private function createDeleteForm(Campagne $campagne)
{
    return $this->createFormBuilder()
        ->setAction($this->generateUrl('campagne_delete', array('id' => $campagne->getId())))
        ->setMethod('DELETE')
        ->getForm()
    ;
}
 }

用户控制器:

class UserController extends Controller
 {
/**
 * Lists all user entities.
 *
 */
public function indexAction()
{
    $em = $this->getDoctrine()->getManager();

    $users = $em->getRepository('XxBundle:User')->findAll();

    return $this->render('user/index.html.twig', array(
        'users' => $users,
    ));
}

/**
 * Creates a new user entity.
 *
 */
public function newAction(Request $request)
{
    $user = new User();
    $form = $this->createForm('XxBundle\Form\UserType', $user);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($user);
        $em->flush();

        return $this->redirectToRoute('user_show', array('id' => $user->getId()));
    }

    return $this->render('user/new.html.twig', array(
        'user' => $user,
        'form' => $form->createView(),
    ));
}

/**
 * Finds and displays a user entity.
 *
 */
public function showAction(User $user)
{
    $deleteForm = $this->createDeleteForm($user);

    return $this->render('user/show.html.twig', array(
        'user' => $user,
        'delete_form' => $deleteForm->createView(),
    ));
}

/**
 * Displays a form to edit an existing user entity.
 *
 */
public function editAction(Request $request, User $user)
{
    $deleteForm = $this->createDeleteForm($user);
    $editForm = $this->createForm('XxBundle\Form\UserType', $user);
    $editForm->handleRequest($request);

    if ($editForm->isSubmitted() && $editForm->isValid()) {
        $this->getDoctrine()->getManager()->flush();

        return $this->redirectToRoute('user_edit', array('id' => $user->getId()));
    }

    return $this->render('user/edit.html.twig', array(
        'user' => $user,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}

/**
 * Deletes a user entity.
 *
 */
public function deleteAction(Request $request, User $user)
{
    $form = $this->createDeleteForm($user);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->remove($user);
        $em->flush();
    }

    return $this->redirectToRoute('user_index');
}

/**
 * Creates a form to delete a user entity.
 *
 * @param User $user The user entity
 *
 * @return \Symfony\Component\Form\Form The form
 */
private function createDeleteForm(User $user)
{
    return $this->createFormBuilder()
        ->setAction($this->generateUrl('user_delete', array('id' => $user->getId())))
        ->setMethod('DELETE')
        ->getForm()
    ;
}
 }

1 个答案:

答案 0 :(得分:1)

您在数据库表上遇到了NOT NULL约束。您要么需要更改表以允许插入空值,要么确保您始终在请求中传递user_id