Search code examples
utf-8doctrine-ormutf8mb4

Doctrine is converting string to hex value


I am converting one database to another database through a script.

While running the script I am getting the following error:

SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xBFbangl...' for column 'country' at row 1  

An exception is thrown where it shows that it tries to set country as "\xcf\xbb\xbf\x62\x61\x6e\x67\x6c\x61\x64\x65\x73\x68".

When I var_dump the value, it just shows as "Bangladesh".

Is there something I have to adjust in my php code?

I have tried this, but phpmyadmin is throwing an #1064 error stating a syntax error in the query.

UPDATE

The scripts is a command in 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);

            }

        }
    }
}

Solution

  • Ok after days of trying this did the trick:

    $country = iconv("UTF-8", "ISO-8859-1//IGNORE", $country);

    I have no idea why it went wrong in the first place. if anybody knows, please share.