我正在尝试从默认控制器内部创建表单。我希望主页中两个下拉列表的值都存储在事件表的ET1和ET2列中,我可以做到这一点。但是,我希望登录用户的user_id也应该存储在事件表格的user_ids列中。在尝试这样做的时候,它给了我一个错误:
执行"INSERT INTO事件(user_id,ET1,ET2)VALUES(?,?,?)'with params[null,2,3]:SQLSTATE[23000]:完整性约束冲突:1048列"user_id"不能为空
这是我的defaultcontroller 代码
<?php
namespace AppBundleController;
use AppBundleEntityevents;
//use AppBundleEntityeventtype;
use AppBundleEntityusers;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentFormExtensionCoreTypeSubmitType;
use SymfonyComponentFormExtensionCoreTypeChoiceType;
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 AppBundleEntity;
use FOSUserBundleModelUser as BaseUser;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use DoctrineCommonCollectionsArrayCollection;
/**
* users
*
* @ORMTable(name="users")
* @ORMEntity(repositoryClass="AppBundleRepositoryusersRepository")
*/
class users extends BaseUser
{
/**
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* Get id
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @ORMOneToMany(targetEntity="events", mappedBy="users")
*/
protected $multiEvents;
public function __construct()
{
parent::__construct();
$this->multiEvents = new ArrayCollection();
}
}
以下是事件实体
<?php
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
/**
* events
*
* @ORMTable(name="events")
* @ORMEntity(repositoryClass="AppBundleRepositoryeventsRepository")
*/
class events
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var int
*
* @ORMColumn(name="user_id", type="integer")
*/
protected $user_id;
/**
* @var int
*
* @ORMColumn(name="ET1", type="integer")
*/
protected $eT1;
/**
* @var int
*
* @ORMColumn(name="ET2", type="integer")
*/
protected $eT2;
/**
* @ORMManyToOne(targetEntity="users", inversedBy="multievents")
* @ORMJoinColumn(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;
}
}
您需要在events
上设置user
对象,而不是设置user_id
。
因此,在events
类的singleUser
上公开setter(您可以删除已经定义为relationship的user_id属性的定义)
事件
public function setUser($user)
{
this->singleUser = $user;
}
并在控制器中直接设置用户对象:
if ($form->isValid()) {
$user = $this->container->get('security.context')->getToken()->getUser();
$events->setuser($user);
...
希望这能帮助