到目前为止,我整天都在寻找我的问题的答案而没有成功。我想从Doctrine2生成的表单中获取数据并将其保存到Symfony2项目的SQL数据库中。这不起作用,我每次都面对这个例外:
An exception occurred while executing 'INSERT INTO mail (title, body, from, to, time) VALUES (?, ?, ?, ?, ?)'
with params ["hello", "test", "charlie", "sepp", "2013-08-29 17:09:01"]:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near 'from, to, time)
VALUES ('hello', 'test', 'charlie', 'sepp', '2013-08-29 17:09:01'' at line 1
实体:
<?php
namespace daniel\mydemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="mail")
*/
class Mail {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
/**
* @ORM\Column(type="string", length=200)
*/
protected $title;
/**
* @ORM\Column(type="text")
*/
protected $body;
/**
* @ORM\Column(type="string", length=100)
*/
protected $from;
/**
* @ORM\Column(type="string", length=100)
*/
protected $to;
/**
* @ORM\Column(type="datetime")
*/
protected $time;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set to
*
* @param string $to
* @return Mail
*/
public function setTo($to)
{
$this->to = $to;
return $this;
}
/**
* Get to
*
* @return string
*/
public function getTo()
{
return $this->to;
}
/**
* Set from
*
* @param string $from
* @return Mail
*/
public function setFrom($from)
{
$this->from = $from;
return $this;
}
/**
* Get from
*
* @return string
*/
public function getFrom()
{
return $this->from;
}
/**
* Set time
*
* @param \DateTime $time
* @return Mail
*/
public function setTime($time)
{
$this->time = $time;
return $this;
}
/**
* Get time
*
* @return \DateTime
*/
public function getTime()
{
return $this->time;
}
/**
* Set title
*
* @param string $title
* @return Mail
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set body
*
* @param string $body
* @return Mail
*/
public function setBody($body)
{
$this->body = $body;
return $this;
}
/**
* Get body
*
* @return string
*/
public function getBody()
{
return $this->body;
}
}
<?php
namespace daniel\mydemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="mail")
*/
class Mail {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
/**
* @ORM\Column(type="string", length=200)
*/
protected $title;
/**
* @ORM\Column(type="text")
*/
protected $body;
/**
* @ORM\Column(type="string", length=100)
*/
protected $from;
/**
* @ORM\Column(type="string", length=100)
*/
protected $to;
/**
* @ORM\Column(type="datetime")
*/
protected $time;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set to
*
* @param string $to
* @return Mail
*/
public function setTo($to)
{
$this->to = $to;
return $this;
}
/**
* Get to
*
* @return string
*/
public function getTo()
{
return $this->to;
}
/**
* Set from
*
* @param string $from
* @return Mail
*/
public function setFrom($from)
{
$this->from = $from;
return $this;
}
/**
* Get from
*
* @return string
*/
public function getFrom()
{
return $this->from;
}
/**
* Set time
*
* @param \DateTime $time
* @return Mail
*/
public function setTime($time)
{
$this->time = $time;
return $this;
}
/**
* Get time
*
* @return \DateTime
*/
public function getTime()
{
return $this->time;
}
/**
* Set title
*
* @param string $title
* @return Mail
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set body
*
* @param string $body
* @return Mail
*/
public function setBody($body)
{
$this->body = $body;
return $this;
}
/**
* Get body
*
* @return string
*/
public function getBody()
{
return $this->body;
}
}
控制器:
<?php
namespace daniel\mydemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form;
use daniel\mydemoBundle\Entity\Mail;
use daniel\mydemoBundle\Form\MailType;
class MailController extends Controller {
public function newAction() {
$mail = new Mail();
$form = $this->createForm(new MailType(), $mail);
return $this->render('danielmydemoBundle:Mail:form.html.twig', array(
'mail' => $mail,
'form' => $form->createView()
));
}
public function sendAction() {
$mail = new Mail();
$user = $this->getUser()->getUsername();
$mail->setFrom($user);
$mail->setTime(new \DateTime());
$request = $this->getRequest();
$form = $this->createForm(new MailType(), $mail);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($mail);
$em->flush();
return $this->redirect($this->generateUrl('danielmydemo_homepage'));
}
return $this->render('danielmydemoBundle:Mail:sendmail.html.twig', array(
'mail' => $mail,
'form' => $form
));
}
public function showAction()
{
$em = $this->getDoctrine()
->getManager();
$mails = $em->createQueryBuilder()
->select('b')
->from('danielmydemoBundle:Mail', 'b')
->addOrderBy('b.time', 'DESC')
->getQuery()
->getResult();
return $this->render('danielmydemoBundle:Mail:mailbox.html.twig', array('mails' => $mails));
}
}
FormType:
<?php
namespace daniel\mydemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form;
use daniel\mydemoBundle\Entity\Mail;
use daniel\mydemoBundle\Form\MailType;
class MailController extends Controller {
public function newAction() {
$mail = new Mail();
$form = $this->createForm(new MailType(), $mail);
return $this->render('danielmydemoBundle:Mail:form.html.twig', array(
'mail' => $mail,
'form' => $form->createView()
));
}
public function sendAction() {
$mail = new Mail();
$user = $this->getUser()->getUsername();
$mail->setFrom($user);
$mail->setTime(new \DateTime());
$request = $this->getRequest();
$form = $this->createForm(new MailType(), $mail);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($mail);
$em->flush();
return $this->redirect($this->generateUrl('danielmydemo_homepage'));
}
return $this->render('danielmydemoBundle:Mail:sendmail.html.twig', array(
'mail' => $mail,
'form' => $form
));
}
public function showAction()
{
$em = $this->getDoctrine()
->getManager();
$mails = $em->createQueryBuilder()
->select('b')
->from('danielmydemoBundle:Mail', 'b')
->addOrderBy('b.time', 'DESC')
->getQuery()
->getResult();
return $this->render('danielmydemoBundle:Mail:mailbox.html.twig', array('mails' => $mails));
}
}
我的SQL表:
<?php
namespace daniel\mydemoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class MailType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('body')
//->add('from')
->add('to')
//->add('time')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'daniel\mydemoBundle\Entity\Mail'
));
}
/**
* @return string
*/
public function getName()
{
return 'daniel_mydemobundle_mail';
}
}
思想:
如果我发表评论<?php
namespace daniel\mydemoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class MailType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('body')
//->add('from')
->add('to')
//->add('time')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'daniel\mydemoBundle\Entity\Mail'
));
}
/**
* @return string
*/
public function getName()
{
return 'daniel_mydemobundle_mail';
}
}
,我会被重定向到主页。但是,该条目不会被保存。因此,我假设输入被正确识别,问题似乎与SQL语句或表格有关。
我在项目中发生了另一个非常类似的ORM故事。这可能是个问题吗?
我完全迷失了,并且确定我没有看到明显的。