使用params [null,2,3]执行'INSERT INTO事件(user_id,ET1,ET2)VALUES(?,?,?)'时发生异常:

时间:2016-04-19 10:05:36

标签: php symfony doctrine-orm

我正在尝试从默认控制器内部创建表单。我希望主页中的两个下拉列表中的值都存储在事件表的ET1和ET2列中,我可以这样做。但是,我希望登录用户的user_id也应该存储在events表的user_id列中。在尝试这样做时,它给了我错误:

  

执行'INSERT INTO事件时发生异常(user_id,   ET1,ET2)VALUES(?,?,?)'与params [null,2,3]:SQLSTATE [23000]:   完整性约束违规:1048列'user_id'不能为空

这是我的defaultcontroller代码

<?php

namespace AppBundle\Controller;

use AppBundle\Entity\events;
//use AppBundle\Entity\eventtype;
use AppBundle\Entity\users;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

class DefaultController extends Controller {

    /**
     * @Route("/home", name="homepage")
     */
    public function indexAction(Request $request) {
        $events = new events();

        $form = $this->createFormBuilder($events)
                ->add('eT1', ChoiceType::class, array(
                    'choices' => array(
                        'Poker' => 1,
                        'Chess' => 2,
                        'Cricket' => 3,
                        'Marbles' => 4,
                        'Football' => 5,
                    ),
                    'choices_as_values' => true,
                ))
                ->add('eT2', ChoiceType::class, array(
                    'choices' => array(
                        'Poker' => 1,
                        'Chess' => 2,
                        'Cricket' => 3,
                        'Marbles' => 4,
                        'Football' => 5,
                    ),
                    'choices_as_values' => true,
                ))
                ->add('save', SubmitType::class, array('label' => 'Submit'))
                ->getForm();

        if ($request->isMethod('POST')) {
            $form->submit($request);

            if ($form->isValid()) {
                // perform some action, eg. persisting the data to database...
                $user = $this->container->get('security.context')->getToken()->getUser();
                $id = $user->getId();
//               var_dump($id);
//                exit;
                $events->setuserID($id);


                $em = $this->getDoctrine()->getManager();

                // tells Doctrine you want to (eventually) save the Product (no queries yet)
                $em->persist($events);

                // actually executes the queries (i.e. the INSERT query)
                $em->flush();
                return $this->redirectToRoute('homepage');
            }
        }

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

以下是用户etity

<?php

namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * users
 *
 * @ORM\Table(name="users")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\usersRepository")
 */
class users extends BaseUser
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

        /**
     * Get id
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @ORM\OneToMany(targetEntity="events", mappedBy="users")
     */
    protected $multiEvents;


    public function __construct()
    {
        parent::__construct();
        $this->multiEvents = new ArrayCollection();   
         }

    }

以下是事件实体

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * events
 *
 * @ORM\Table(name="events")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\eventsRepository")
 */
class events
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var int
     *
     * @ORM\Column(name="user_id", type="integer")
     */
    protected $user_id;

    /**
     * @var int
     *
     * @ORM\Column(name="ET1", type="integer")
     */
    protected $eT1;

    /**
     * @var int
     *
     * @ORM\Column(name="ET2", type="integer")
     */
    protected $eT2;

    /**
     * @ORM\ManyToOne(targetEntity="users", inversedBy="multievents")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $singleUser;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Get user_id
     *
     * @return integer 
     */
    public function getuser_id()
    {
        return $this->user_id;
    }

    /**
     * Set user_id
     *
     * @param integer $user_id
     * @return events
     */
    public function setuserID($user_id)
    {
        $this->user_id = $user_id;

        return $this;
    }

    /**
     * Set eT1
     *
     * @param integer $eT1
     * @return events
     */
    public function setET1($eT1)
    {
        $this->eT1 = $eT1;

        return $this;
    }

    /**
     * Get eT1
     *
     * @return integer 
     */
    public function getET1()
    {
        return $this->eT1;
    }

    /**
     * Set eT2
     *
     * @param integer $eT2
     * @return events
     */
    public function setET2($eT2)
    {
        $this->eT2 = $eT2;

        return $this;
    }

    /**
     * Get eT2
     *
     * @return integer 
     */
    public function getET2()
    {
        return $this->eT2;
    }

}

1 个答案:

答案 0 :(得分:1)

您需要在user_id上设置user对象,而不是设置events

因此,在singleUserevents上公开setter(您可以删除已定义为关系的user_id属性的定义)

事件

public function setUser($user)
{
 this->singleUser = $user;
}

直接在控制器中设置用户对象:

if ($form->isValid()) {
        $user = $this->container->get('security.context')->getToken()->getUser();
        $events->setuser($user);
  ...

希望这个帮助