将重音字符 é 插入 Symfony2 中的表单时,如何在 mysql 数据库中将重音字符 é 保存为 é?
Posted
技术标签:
【中文标题】将重音字符 é 插入 Symfony2 中的表单时,如何在 mysql 数据库中将重音字符 é 保存为 é?【英文标题】:How to save the accent character é as é in a mysql database when it is inserted in a form in Symfony2 ? 【发布时间】:2014-12-25 10:34:10 【问题描述】:我想知道如何在 mysql 数据库中将重音字符 é 保存为 é,当它插入 Symfony2 中的表单时。解释如下:
这是我拥有的表单生成器的代码:
<?php
namespace Ikproj\GroupeBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class MessagesType extends AbstractType
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
$builder->add('contenu','textarea', array('attr' => array('rows' => '6','cols' => '40')));
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
$resolver->setDefaults(array(
'data_class' => 'Ikproj\GroupeBundle\Entity\Messages'
));
/**
* @return string
*/
public function getName()
return 'ikproj_groupebundle_messages';
这是属于上述表单构建器的实体的代码:
<?php
namespace Ikproj\GroupeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Messages
*
* @ORM\Table(name="messages")
* @ORM\Entity(repositoryClass="Ikproj\GroupeBundle\Entity\MessagesRepository")
*/
class Messages
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="actor", type="integer")
*/
private $actor;
/**
* @var integer
*
* @ORM\Column(name="subject", type="integer")
*/
private $subject;
/**
* @var string
*
* @ORM\Column(name="contenu", type="string")
*/
private $contenu;
/**
* @var string
*
* @ORM\Column(name="status", type="string", length=10)
*/
private $status;
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=10)
*/
private $type;
/**
* Get id
*
* @return integer
*/
public function getId()
return $this->id;
/**
* Set actor
*
* @param integer $actor
* @return Messages
*/
public function setActor($actor)
$this->actor = $actor;
return $this;
/**
* Get actor
*
* @return integer
*/
public function getActor()
return $this->actor;
/**
* Set subject
*
* @param integer $subject
* @return Messages
*/
public function setSubject($subject)
$this->subject = $subject;
return $this;
/**
* Get subject
*
* @return integer
*/
public function getSubject()
return $this->subject;
/**
* Set contenu
*
* @param string $contenu
* @return Messages
*/
public function setContenu($contenu)
$this->contenu = $contenu;
return $this;
/**
* Get contenu
*
* @return string
*/
public function getContenu()
return $this->contenu;
/**
* Set status
*
* @param string $status
* @return Messages
*/
public function setStatus($status)
$this->status = $status;
return $this;
/**
* Get status
*
* @return string
*/
public function getStatus()
return $this->status;
/**
* Set type
*
* @param string $type
* @return Messages
*/
public function setType($type)
$this->type = $type;
return $this;
/**
* Get type
*
* @return string
*/
public function getType()
return $this->type;
这是我拥有的控制器的代码:
public function SendMessageAction(Request $request, $actor, $subject, $cible)
$message = new Messages();
$form = $this->createForm(new MessagesType(), $message);
$em = $this->getDoctrine()->getManager();
$actor1 = $actor;
$subject1 = $subject;
$status1 = "unseen";
$cible1 = $cible;
if ($request->isMethod('POST'))
$form->handleRequest($request);
if ($form->isValid())
$message->setActor($actor1);
$message->setSubject($subject1);
$message->setStatus($status1);
$message->setType($cible1);
$em->persist($message);
$em->flush();
return $this->redirect($this->generateUrl('task_success'));
else
return $this->render('IkprojGroupeBundle:Messages:SendMessage.html.twig', array(
'form' => $form->createView(),
'actor' => $actor,
'subject' => $subject,
'cible' => $cible
));
这是属于教义配置的文件config.yml的代码:
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
问题是,当我在表单中插入重音字符 é 时,我有效,它将在数据库中保存为 é 而不是 é,如下面的屏幕截图所示:
而当我在 Symfony2 中运行以下代码时:
<?php
$tab = $_REQUEST['table'];
$a = $tab[0];
$b = $tab[1];
$con=mysqli_connect("localhost","root","","wkayetdb");
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$result = mysqli_query($con,"INSERT INTO messages(actor,subject,contenu,status,type) VALUES ($a,$b,'é','unseen','G')");
mysqli_close($con);
?>
它可以正常工作,字符 é 将保存为 é,如下所示:
所以,我的问题是:
-
我的代码有什么问题?
在 Symfony2 中将 é 插入表单后,如何在 mysql 数据库中将 é 保存为 é?
【问题讨论】:
【参考方案1】:我不知道你为什么要这样做 但如果你想这样做,只需注释该行“charset:UTF8”
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
# charset: UTF8
【讨论】:
以上是关于将重音字符 é 插入 Symfony2 中的表单时,如何在 mysql 数据库中将重音字符 é 保存为 é?的主要内容,如果未能解决你的问题,请参考以下文章