Symfony 使用关系和表单数据持久化实体
我有一个AppBundle\Entity\Comments实体,当用户填写表单时,我想将它保存到数据库中。我找不到如何将表单数据保存到数据库的确切信息:Symfony 使用关系和表单数据持久化实体,symfony,doctrine-orm,Symfony,Doctrine Orm,我有一个AppBundle\Entity\Comments实体,当用户填写表单时,我想将它保存到数据库中。我找不到如何将表单数据保存到数据库的确切信息: <?php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="Comments") */ class Comments { /** * @ORM\Col
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="Comments")
*/
class Comments
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Employer")
* @ORM\JoinColumn(name="employer_id", referencedColumnName="id")
*/
private $employer;
/**
* @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User")
* @ORM\JoinColumn(name="company_id", referencedColumnName="id")
*/
private $company;
/**
* @ORM\ManyToOne(targetEntity="Commenttypes")
* @ORM\JoinColumn(name="comment_id", referencedColumnName="id")
*/
private $comment;
/**
* @ORM\Column(type="date")
*/
private $from_date;
/**
* @ORM\Column(type="date")
*/
private $to_date;
?>
但我得到了这个错误:
Catchable Fatal Error: Argument 1 passed to AppBundle\Entity\Comments::setEmployer() must be an instance of AppBundle\Entity\Employer, string given, called in /home1/rabotnici/src/AppBundle/Controller/Worker.php on line 42 and defined
你有没有试过这些代码?当您提交表单并将其绑定到
Comments
实体时,在调用handleRequest()
后,已在该$comment
变量中设置提交的表单值。您可以自己检查实体以确认这一点。对于当前登录的用户,可以使用$this->getUser()代码>在控制器中。我还会考虑使用条令自动生成实体,或者将变量名更改为$camelCase,例如$fromDate
和$toDate
。我不知道setEmployer
调用来自何方,但是您不需要显式调用最后三个set*
调用,因为您的实体已经绑定到表单提交。您是否尝试过在提交后转储数据并进行调试以查看实体中的实际内容?对于雇主,您需要检索雇主实体-但这是基于什么?是当前用户的雇主吗?它会是从URL或表单中检索到的雇主吗?现在,当它需要整个对象时,您正在尝试向它传递一个id。它是雇主id,我在$id变量中将它作为整数。在这种情况下,您需要执行$employer=$this->getDoctrine()->getManager()->get('AppBundle:employer')->find($id)代码>然后$comment->setEmployer($employer)代码>您尝试过这些代码吗?当您提交表单并将其绑定到Comments
实体时,在调用handleRequest()
后,已在该$comment
变量中设置提交的表单值。您可以自己检查实体以确认这一点。对于当前登录的用户,可以使用$this->getUser()代码>在控制器中。我还会考虑使用条令自动生成实体,或者将变量名更改为$camelCase,例如$fromDate
和$toDate
。我不知道setEmployer
调用来自何方,但是您不需要显式调用最后三个set*
调用,因为您的实体已经绑定到表单提交。您是否尝试过在提交后转储数据并进行调试以查看实体中的实际内容?对于雇主,您需要检索雇主实体-但这是基于什么?是当前用户的雇主吗?它会是从URL或表单中检索到的雇主吗?现在,当它需要整个对象时,您正在尝试向它传递一个id。它是雇主id,我在$id变量中将它作为整数。在这种情况下,您需要执行$employer=$this->getDoctrine()->getManager()->get('AppBundle:employer')->find($id)代码>然后$comment->setEmployer($employer)代码>
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\Comments;
use AppBundle\Form\Type\WorkerType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class Worker extends Controller
{
/**
* @Route("/worker/{id}", name="worker")
*/
public function indexAction($id, Request $request)
{
$comment = new Comments();
$form = $this->createForm('AppBundle\Form\Type\WorkerType', $comment);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
{{{{ CODE HERE }}}}
}
return $this->render('default/worker.html.twig',array(
'form' => $form->createView()
));
}
}
if ($form->isSubmitted() && $form->isValid()) {
$comment->setEmployer($id);
$comment->setCompany($this->getUser());
$comment->setComment($form->get('comment')->getData());
$comment->setfrom_date($form->get('from_date')->getData());
$comment->setto_date($form->get('to_date')->getData());
}
Catchable Fatal Error: Argument 1 passed to AppBundle\Entity\Comments::setEmployer() must be an instance of AppBundle\Entity\Employer, string given, called in /home1/rabotnici/src/AppBundle/Controller/Worker.php on line 42 and defined