在控制器symfony3中自动设置一对一关系中的关系值
Automatically set relationship value in one-to-one relationship in controller symfony3
一些背景信息。我有两个具有双向关系的实体:本质上,一个申请表可以有一个推荐表。当推荐人填写表格时,我希望自动填写,并且只有 select 相关的申请表格(保护其他用户的数据),基于他们在我创建的自定义表格页面上输入的特定代码。我不确定它是怎么发生的,但我让它工作了一次,但由于某种未知原因,它停止了,并在我的数据库的推荐表中为申请人表 ID 添加了 "NULL"。我会尽我所能提供所有信息:
RecommendationForm 实体:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* RecommendationForm
*
* @ORM\Table(name="recommendation_form")
* @ORM\Entity(repositoryClass="AppBundle\Repository\RecommendationFormRepository")
*/
class RecommendationForm
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* One RecommendationForm has One ApplicantForm.
* @ORM\OneToOne(targetEntity="ApplicantForm", inversedBy="recommendation_form")
* @ORM\JoinColumn(name="applicant_form_id", referencedColumnName="id")
*/
private $applicant_form;
//....
public function __construct($applicant_form) {
date_default_timezone_set('America/New_York');
$this->applicant_form = $applicant_form;
$this->submitDate = new \DateTime("now");
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
// /**
// * Set applicantForm
// *
// * @param integer $applicant_form
// *
// * @return RecommendationForm
// */
// public function setApplicantForm($applicant_form)
// {
// $this->applicant_form = $applicant_form;
// return $this;
// }
/**
* Get applicantForm
*
* @return integer
*/
public function getApplicantForm()
{
return $this->applicant_form;
}
//....
}
申请表实体:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use chriskacerguis\Randomstring\Randomstring;
/**
* ApplicantForm
*
* @ORM\Table(name="applicant_form")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ApplicantFormRepository")
*/
class ApplicantForm
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* One ApplicantForm has One RecommendationForm.
* @ORM\OneToOne(targetEntity="RecommendationForm", mappedBy="applicant_form")
*/
private $recommendation_form;
/**
* @var string
*
* @ORM\Column(name="accessCode", type="string", length=75, unique=true)
*/
private $accessCode;
//....
public function __construct() {
$random = new \chriskacerguis\Randomstring\Randomstring();
$this->accessCode = $random->generate(15, true);
}
//...
}
推荐控制器:
namespace AppBundle\Controller;
use AppBundle\Entity\RecommendationForm;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
/**
* Recommendationform controller.
*
* @Route("recommendationform")
*/
class RecommendationFormController extends Controller
{
private $accessCode;
private $applicantForm;
private $applicantFormID;
/**
* Lists all RecommendationForm entities.
*
* @Route("/", name="recommendationform_index")
* @Method({"GET", "POST"})
*/
public function indexAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$data = array();
$form = $this->createFormBuilder($data)
->add('email', EmailType::class)
->add('accessCodeSS', PasswordType::class)
->add('submit', SubmitType::class)
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$accessCode = $form["accessCodeSS"]->getData();
return $this->redirectToRoute('recommendationform_new');
}
return $this->render('recommendationform/index.html.twig', array(
'form' => $form->createView(),
));
}
/**
* Creates a new RecommendationForm entity.
*
* @Route("/new", name="recommendationform_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
if(isset($_POST["accessCodeSS"])){
$this->accessCode = $_POST["accessCodeSS"];
$accessCode = $this->accessCode;
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery('SELECT u FROM AppBundle:ApplicantForm u WHERE u.accessCode = :accessCode')
->setParameter('accessCode', $accessCode);
$applicantFormID = $query->getResult();
$this->applicantForm = $applicantFormID[0];
}
var_dump($this->applicantForm);
$RecommendationForm = new Recommendationform($this->applicantForm);
$form = $this->createForm('AppBundle\Form\RecommendationFormType', $RecommendationForm);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($RecommendationForm);
$em->flush();
return $this->redirectToRoute('recommendationform_show', array('id' => $RecommendationForm->getId()));
}
return $this->render('recommendationform/new.html.twig', array(
'RecommendationForm' => $RecommendationForm,
'form' => $form->createView(),
));
}
/**
* Finds and displays a RecommendationForm entity.
*
* @Route("/{id}", name="recommendationform_show")
* @Method("GET")
*/
public function showAction(RecommendationForm $RecommendationForm)
{
$deleteForm = $this->createDeleteForm($RecommendationForm);
return $this->render('recommendationform/show.html.twig', array(
'RecommendationForm' => $RecommendationForm,
'delete_form' => $deleteForm->createView(),
));
}
/**
* Displays a form to edit an existing RecommendationForm entity.
*
* @Route("/{id}/edit", name="recommendationform_edit")
* @Method({"GET", "POST"})
*/
public function editAction(Request $request, RecommendationForm $RecommendationForm)
{
$deleteForm = $this->createDeleteForm($RecommendationForm);
$editForm = $this->createForm('AppBundle\Form\RecommendationFormType', $RecommendationForm);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('recommendationform_edit', array('id' => $RecommendationForm->getId()));
}
return $this->render('recommendationform/edit.html.twig', array(
'RecommendationForm' => $RecommendationForm,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a RecommendationForm entity.
*
* @Route("/{id}", name="recommendationform_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, RecommendationForm $RecommendationForm)
{
$form = $this->createDeleteForm($RecommendationForm);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($RecommendationForm);
$em->flush();
}
return $this->redirectToRoute('recommendationform_index');
}
/**
* Creates a form to delete a RecommendationForm entity.
*
* @param RecommendationForm $RecommendationForm The RecommendationForm entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm(RecommendationForm $RecommendationForm)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('recommendationform_delete', array('id' => $RecommendationForm->getId())))
->setMethod('DELETE')
->getForm()
;
}
}
我在整个互联网上搜索过,但找不到答案或弄清楚它是如何停止工作的。
我想出了我改变了什么,我会把答案放在这里以防有人想要这样做。我正在尝试使用不同的表单方法并将表单的原始方法从 "GET" 更改为 POST (并且对控制器中值的所有调用都从 "GET" 更改为 "POST").一时兴起,我把它改回来,它又开始工作了。我很确定我更改了它,因为我不喜欢 get 方法将查询字符串放在地址栏中,并错误地认为将方法更改为 post 会修复它。如果有人推荐使用什么代替那个,我欢迎所有建议。
控制器不工作:
//...
public function newAction(Request $request)
{
if(isset($_POST["accessCodeSS"])){
$this->accessCode = $_POST["accessCodeSS"];
//...
}
正确的工作控制器:
public function newAction(Request $request)
{
if(isset($_GET["accessCodeSS"])){
$this->accessCode = $_GET["accessCodeSS"];
//...
}
正确形式(index.html.twig):
<form action="{{ path('recommendation_form_new') }}" method="GET">
<input type="email" name="email" placeholder="Email Address">
<input type="password" name="accessCodeSS" placeholder="Access Code">
<input type="submit" name="submit" value="Submit">
</form>
一些背景信息。我有两个具有双向关系的实体:本质上,一个申请表可以有一个推荐表。当推荐人填写表格时,我希望自动填写,并且只有 select 相关的申请表格(保护其他用户的数据),基于他们在我创建的自定义表格页面上输入的特定代码。我不确定它是怎么发生的,但我让它工作了一次,但由于某种未知原因,它停止了,并在我的数据库的推荐表中为申请人表 ID 添加了 "NULL"。我会尽我所能提供所有信息:
RecommendationForm 实体:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* RecommendationForm
*
* @ORM\Table(name="recommendation_form")
* @ORM\Entity(repositoryClass="AppBundle\Repository\RecommendationFormRepository")
*/
class RecommendationForm
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* One RecommendationForm has One ApplicantForm.
* @ORM\OneToOne(targetEntity="ApplicantForm", inversedBy="recommendation_form")
* @ORM\JoinColumn(name="applicant_form_id", referencedColumnName="id")
*/
private $applicant_form;
//....
public function __construct($applicant_form) {
date_default_timezone_set('America/New_York');
$this->applicant_form = $applicant_form;
$this->submitDate = new \DateTime("now");
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
// /**
// * Set applicantForm
// *
// * @param integer $applicant_form
// *
// * @return RecommendationForm
// */
// public function setApplicantForm($applicant_form)
// {
// $this->applicant_form = $applicant_form;
// return $this;
// }
/**
* Get applicantForm
*
* @return integer
*/
public function getApplicantForm()
{
return $this->applicant_form;
}
//....
}
申请表实体:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use chriskacerguis\Randomstring\Randomstring;
/**
* ApplicantForm
*
* @ORM\Table(name="applicant_form")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ApplicantFormRepository")
*/
class ApplicantForm
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* One ApplicantForm has One RecommendationForm.
* @ORM\OneToOne(targetEntity="RecommendationForm", mappedBy="applicant_form")
*/
private $recommendation_form;
/**
* @var string
*
* @ORM\Column(name="accessCode", type="string", length=75, unique=true)
*/
private $accessCode;
//....
public function __construct() {
$random = new \chriskacerguis\Randomstring\Randomstring();
$this->accessCode = $random->generate(15, true);
}
//...
}
推荐控制器:
namespace AppBundle\Controller;
use AppBundle\Entity\RecommendationForm;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
/**
* Recommendationform controller.
*
* @Route("recommendationform")
*/
class RecommendationFormController extends Controller
{
private $accessCode;
private $applicantForm;
private $applicantFormID;
/**
* Lists all RecommendationForm entities.
*
* @Route("/", name="recommendationform_index")
* @Method({"GET", "POST"})
*/
public function indexAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$data = array();
$form = $this->createFormBuilder($data)
->add('email', EmailType::class)
->add('accessCodeSS', PasswordType::class)
->add('submit', SubmitType::class)
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$accessCode = $form["accessCodeSS"]->getData();
return $this->redirectToRoute('recommendationform_new');
}
return $this->render('recommendationform/index.html.twig', array(
'form' => $form->createView(),
));
}
/**
* Creates a new RecommendationForm entity.
*
* @Route("/new", name="recommendationform_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
if(isset($_POST["accessCodeSS"])){
$this->accessCode = $_POST["accessCodeSS"];
$accessCode = $this->accessCode;
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery('SELECT u FROM AppBundle:ApplicantForm u WHERE u.accessCode = :accessCode')
->setParameter('accessCode', $accessCode);
$applicantFormID = $query->getResult();
$this->applicantForm = $applicantFormID[0];
}
var_dump($this->applicantForm);
$RecommendationForm = new Recommendationform($this->applicantForm);
$form = $this->createForm('AppBundle\Form\RecommendationFormType', $RecommendationForm);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($RecommendationForm);
$em->flush();
return $this->redirectToRoute('recommendationform_show', array('id' => $RecommendationForm->getId()));
}
return $this->render('recommendationform/new.html.twig', array(
'RecommendationForm' => $RecommendationForm,
'form' => $form->createView(),
));
}
/**
* Finds and displays a RecommendationForm entity.
*
* @Route("/{id}", name="recommendationform_show")
* @Method("GET")
*/
public function showAction(RecommendationForm $RecommendationForm)
{
$deleteForm = $this->createDeleteForm($RecommendationForm);
return $this->render('recommendationform/show.html.twig', array(
'RecommendationForm' => $RecommendationForm,
'delete_form' => $deleteForm->createView(),
));
}
/**
* Displays a form to edit an existing RecommendationForm entity.
*
* @Route("/{id}/edit", name="recommendationform_edit")
* @Method({"GET", "POST"})
*/
public function editAction(Request $request, RecommendationForm $RecommendationForm)
{
$deleteForm = $this->createDeleteForm($RecommendationForm);
$editForm = $this->createForm('AppBundle\Form\RecommendationFormType', $RecommendationForm);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('recommendationform_edit', array('id' => $RecommendationForm->getId()));
}
return $this->render('recommendationform/edit.html.twig', array(
'RecommendationForm' => $RecommendationForm,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a RecommendationForm entity.
*
* @Route("/{id}", name="recommendationform_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, RecommendationForm $RecommendationForm)
{
$form = $this->createDeleteForm($RecommendationForm);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($RecommendationForm);
$em->flush();
}
return $this->redirectToRoute('recommendationform_index');
}
/**
* Creates a form to delete a RecommendationForm entity.
*
* @param RecommendationForm $RecommendationForm The RecommendationForm entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm(RecommendationForm $RecommendationForm)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('recommendationform_delete', array('id' => $RecommendationForm->getId())))
->setMethod('DELETE')
->getForm()
;
}
}
我在整个互联网上搜索过,但找不到答案或弄清楚它是如何停止工作的。
我想出了我改变了什么,我会把答案放在这里以防有人想要这样做。我正在尝试使用不同的表单方法并将表单的原始方法从 "GET" 更改为 POST (并且对控制器中值的所有调用都从 "GET" 更改为 "POST").一时兴起,我把它改回来,它又开始工作了。我很确定我更改了它,因为我不喜欢 get 方法将查询字符串放在地址栏中,并错误地认为将方法更改为 post 会修复它。如果有人推荐使用什么代替那个,我欢迎所有建议。
控制器不工作:
//...
public function newAction(Request $request)
{
if(isset($_POST["accessCodeSS"])){
$this->accessCode = $_POST["accessCodeSS"];
//...
}
正确的工作控制器:
public function newAction(Request $request)
{
if(isset($_GET["accessCodeSS"])){
$this->accessCode = $_GET["accessCodeSS"];
//...
}
正确形式(index.html.twig):
<form action="{{ path('recommendation_form_new') }}" method="GET">
<input type="email" name="email" placeholder="Email Address">
<input type="password" name="accessCodeSS" placeholder="Access Code">
<input type="submit" name="submit" value="Submit">
</form>