开发者

Doctrine: how to create a query using "LIKE REPLACE"?

开发者 https://www.devze.com 2023-01-01 14:05 出处:网络
this SQL clause is working OK: SELECT * FROM `sf_guard_user` WHERE nombre_apellidos LIKE REPLACE(\'Mar Sanz\',\' \',\'%\')

this SQL clause is working OK:

SELECT * FROM `sf_guard_user` WHERE nombre_apellidos LIKE REPLACE('Mar Sanz',' ','%')

Now I'm trying to write this query for Doctrine. I have tried this:

$query->andWhere(sprintf('%s.%s LIKE REPLACE (?," ","%开发者_开发百科")',
                         $query->getRootAlias(), $fieldName),
                 'Mar Sanz'));

but I get this error:

Warning: sprintf() [function.sprintf]: Too few arguments

Any idea?

Regards

Javi


A '%' must be doubled ('%%') to mean a literal % inside of a pattern string you sent to sprintf or printf:

$query->andWhere(sprintf('%s.%s LIKE REPLACE (?," ","%%")',
                         $query->getRootAlias(), $fieldName),
                 'Mar Sanz'));

Suggestion: Why not run the string-replacement function inside the code before you send it to the database server?

$query->andWhere(
    sprintf(
        '%s.%s LIKE ?',
        $query->getRootAlias(),
        $fieldName
    ),
    preg_replace('/ /', '%', 'Mar Sanz')
);
0

精彩评论

暂无评论...
验证码 换一张
取 消