utf-8 - Doctrine 正在将字符串转换为十六进制值

标签 utf-8 doctrine-orm utf8mb4

我正在通过脚本将一个数据库转换为另一个数据库。

运行脚本时出现以下错误:

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);

            }

        }
    }
}

最佳答案

\x62\x61\x6e\x67\x6c\x61\x64\x65\x73\x68Bangladesh;之前的\xcf\xbb\xbf 是没有意义的。在 latin1 中,它是 Ï»¿。它不能有效地转换为 utf8。 CFBBϻ (GREEK SMALL LETTER SAN),但是 bf 是 utf8.

我建议它来自数据源。

var_dump 可能没有显示任何内容,因为您在什么设备上显示它。

关于utf-8 - Doctrine 正在将字符串转换为十六进制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38462392/

相关文章:

Android EditText 最大长度计算 UTF-8 编码字符

php - Doctrine2 ORM - 托管+脏实体 X 无法安排插入

php - Doctrine 2 像在数据库中一样使用表列,而不是 camelCase

mysql - 表情符号未插入数据库 Node js mysql

mysql - 将 phpMyAdmin 导出的数据库转换为旧版本。 utf8mb4 问题

python - 如何将utf-8输出重定向到txt文件

javascript - AngularJS 路由中的 Unicode/utf-8 字符

vim - Vim 中包含 ß 的单词

php - 具有 "$fetchJoinCollection = true"的分页器不会在学说 DQL 中遵守 "ORDER BY"?

mysql - 如何最终在 MySQL 上正确设置 utf8mb4?