I've spent hours to debug an algo and noticed it was coming from Doctrine (v2.3.3). I'm using libpuzzle to calculate a hash of an image and store this hash in the database. The hash returned has special character in it and apparently Doctrine doesn't like it.
This is the kind of string I have (~550 char):
ÿ�þÿÿþ�þþÿþÿþÿþþ�þÿþÿÿÿ�ÿþþÿþÿ�ÿÿþÿþÿþþÿþþÿÿÿþÿþþþþÿþ�þþþÿ�ÿÿ�ÿÿþ�þÿþÿþÿþÿþþÿþÿÿÿþþÿþþþþÿþþþþ���ÿÿ�ÿ�þþ�þÿÿþþþÿÿþÿþþÿþþþÿþ...
I've investigated and found some people saying to add the charset in the config but I already have it:
# Doctrine Configuration
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
If I change the collation of the column from utf8_unicode_ci
to utf8_general_ci
still not working. Also I've made sure that the default schema collation is utf8_general_ci
.
I've tried another string with special characters:
Test !§$%&/()=? äöü ÄÖÜ :D
This string is inserted correctly but the hash still not.
Does anyone know where I could look to fix this issue? Is it a Doctrine bug?
----------------------------------
UPDATE
Just before the execute I still have the correct data binded. So I guess it's a encoding or driver issue. I've modified the symfony configuration:
# Doctrine Configuration
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
options:
1002: "SET NAMES 'UTF8'"
I've also tried to execute the query without using entities to be sure:
$this->db = $this->getContainer()->get('doctrine')->getConnection();
$img = '/var/www/acme/web/upload/tmp/cd1fa593cf6feb2cde83e68f461a2d947.jpg';
$hash = puzzle_fill_cvec_from_file($img);
$sql = "UPDATE image set hash=? WHERE id=?";
$stmt = $this->db->prepare($sql);
$stmt->execute(array($hash, 180));
Still empty data in the database.
In another project I've used Zend_Db
and didn't have any trouble saving this hash.
I don't know if it's a bug in Doctrine
or not :(
----------------------------------
UPDATE 2
I've logged all the queries in mysql and I can see in the log that the content is binded correctly. but not saved properly by mysql.
INSERT INTO image (guid, type, createTime, updateTime, images, imageSize, imageHash, status)
VALUES (
'c30df23d6b0b08aff079287e00f21ec8a',
'image',
'2013-04-22 03:30:33',
'2013-04-22 03:30:34',
'path/image.jpg',
'165458',
'?\0????\0?????????\0??????\0??????\0??????????????????????\0????\0??\0???\0???????????????????????????\0\0\0??\0?\0??\0??????????????????\0\0\0??\0?\0???????????????\0?????????????????\0?\0????????????\0?\0??????????????????????????\0?\0???????????\0???\0?????????\0??????\0\0?????????????????????\0\0??????\0???????????????\0',
1)
This is my create table:
delimiter $$
CREATE TABLE `image` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`guid` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`createTime` datetime NOT NULL,
`updateTime` datetime DEFAULT NULL,
`images` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`imageSize` bigint(20) DEFAULT NULL,
`imageHash` longtext COLLATE utf8_unicode_ci,
`status` integer(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci$$
Cheers, Maxime