Doctrine 正在将字符串转换为十六进制值
Doctrine is converting string to hex value
我正在通过脚本将一个数据库转换为另一个数据库。
虽然 运行 脚本出现以下错误:
SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xBFbangl...' for column 'country' at row 1
抛出一个异常,表明它试图将国家/地区设置为“\xcf\xbb\xbf\x62\x61\x6e\x67\x6c\x61\x64\x65\x73\x68”。
当我var_dump值时,它只是显示为"Bangladesh"。
我的 php 代码中有什么需要调整的吗?
我已尝试 this,但 phpmyadmin 抛出 #1064 错误,指出查询中存在语法错误。
更新
脚本是 Symfony3 中的命令:
<?php
namespace My\Bundle\Command;
use My\AccommodationBundle\Entity\Visit;
use My\NewAccommodationBundleV2\Entity\Visit as Visit2;
use My\OtherBundle\Entity\Person;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class VisitConvertToNewFormatCommand extends ContainerAwareCommand
{
private $em;
protected function configure()
{
$this
->setName('accommodation:visit:convert')
->setDescription("Converting old structure to new structure'")
;
}
protected function getTimeStamp() {
$currentTime = new \DateTime('now');
return '['.$currentTime->format('Y-m-d H:i:s').'] ';
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln($this->getTimeStamp().$this->getDescription());
$this->em = $this->getContainer()->get('doctrine')->getManager();
$visits = $this->em->getRepository('MyOldAccommodationBundle:Visit')->findAll();
foreach ($visits as $visit) {
$visit2 = new Visit2();
$visit2->setArrivalDate($visit->getArrivalDate());
$visit2->setArrivalStatus($visit->getArrivalStatus());
$visit2->setArrivalTime($visit->getArrivalTime());
$visit2->setBookingType($visit->getBookingType());
$visit2->setCountry($visit->getCountry()); // <----
...
$this->em->persist($visit2);
$user = $visit->getUser();
if ($user != null) {
$person = new Person();
...
$person->setFirstName(trim(ucfirst(strtolower($user->getFirstName()))));
$person->setLastName(trim(ucfirst(strtolower($user->getLastName()))));
$person->setEmail(preg_replace('/\s+/', '', $user->getEmail()));
...
$this->em->persist($person);
}
}
}
}
好的,经过几天的尝试,成功了:
$国家=iconv("UTF-8","ISO-8859-1//IGNORE",$国家);
我一开始不知道为什么会出错。如果有人知道,请分享。
\x62\x61\x6e\x67\x6c\x61\x64\x65\x73\x68
是 Bangladesh
;之前的 \xcf\xbb\xbf 是没有意义的。在 latin1 中是 Ï»¿
。它不能有效地转换为 utf8。 CFBB
是 ϻ
(希腊小写字母 SAN),但是 bf
是 utf8.
我建议它来自数据源。
var_dump 可能什么都没显示,因为你在什么设备上显示它。
我正在通过脚本将一个数据库转换为另一个数据库。
虽然 运行 脚本出现以下错误:
SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xBFbangl...' for column 'country' at row 1
抛出一个异常,表明它试图将国家/地区设置为“\xcf\xbb\xbf\x62\x61\x6e\x67\x6c\x61\x64\x65\x73\x68”。
当我var_dump值时,它只是显示为"Bangladesh"。
我的 php 代码中有什么需要调整的吗?
我已尝试 this,但 phpmyadmin 抛出 #1064 错误,指出查询中存在语法错误。
更新
脚本是 Symfony3 中的命令:
<?php
namespace My\Bundle\Command;
use My\AccommodationBundle\Entity\Visit;
use My\NewAccommodationBundleV2\Entity\Visit as Visit2;
use My\OtherBundle\Entity\Person;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class VisitConvertToNewFormatCommand extends ContainerAwareCommand
{
private $em;
protected function configure()
{
$this
->setName('accommodation:visit:convert')
->setDescription("Converting old structure to new structure'")
;
}
protected function getTimeStamp() {
$currentTime = new \DateTime('now');
return '['.$currentTime->format('Y-m-d H:i:s').'] ';
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln($this->getTimeStamp().$this->getDescription());
$this->em = $this->getContainer()->get('doctrine')->getManager();
$visits = $this->em->getRepository('MyOldAccommodationBundle:Visit')->findAll();
foreach ($visits as $visit) {
$visit2 = new Visit2();
$visit2->setArrivalDate($visit->getArrivalDate());
$visit2->setArrivalStatus($visit->getArrivalStatus());
$visit2->setArrivalTime($visit->getArrivalTime());
$visit2->setBookingType($visit->getBookingType());
$visit2->setCountry($visit->getCountry()); // <----
...
$this->em->persist($visit2);
$user = $visit->getUser();
if ($user != null) {
$person = new Person();
...
$person->setFirstName(trim(ucfirst(strtolower($user->getFirstName()))));
$person->setLastName(trim(ucfirst(strtolower($user->getLastName()))));
$person->setEmail(preg_replace('/\s+/', '', $user->getEmail()));
...
$this->em->persist($person);
}
}
}
}
好的,经过几天的尝试,成功了:
$国家=iconv("UTF-8","ISO-8859-1//IGNORE",$国家);
我一开始不知道为什么会出错。如果有人知道,请分享。
\x62\x61\x6e\x67\x6c\x61\x64\x65\x73\x68
是 Bangladesh
;之前的 \xcf\xbb\xbf 是没有意义的。在 latin1 中是 Ï»¿
。它不能有效地转换为 utf8。 CFBB
是 ϻ
(希腊小写字母 SAN),但是 bf
是 utf8.
我建议它来自数据源。
var_dump 可能什么都没显示,因为你在什么设备上显示它。