0

Here's the line that's making my query fail.

$query = $query->where('a.field LIKE :keyword OR LEFT(a.otherfield, 3) = LEFT(:keyword, 3)');

I get this error:

[Syntax Error] line 0, col 104: Error: Expected known function, got 'LEFT'

This SQL code works:

SELECT * FROM `table`
WHERE field LIKE 'searchterm'
OR LEFT(`otherfield`, 3) = LEFT('searchterm', 3)

Why does LEFT() return an error? Is there a different way to do it with query builder?

chrislebaron
  • 269
  • 3
  • 14
  • You will have to implement your own DQL function, please see [this](http://www.doctrine-project.org/2010/03/29/doctrine2-custom-dql-udfs.html) – LMS94 Apr 12 '16 at 20:05
  • 1
    Possible duplicate of [Doctrine LEFT mysql function](http://stackoverflow.com/questions/9761748/doctrine-left-mysql-function) – chalasr Apr 12 '16 at 21:20

2 Answers2

1

Try to look at this question. Otherwise, try the old school way! native-sql-with-doctrine.

Community
  • 1
  • 1
Houssem ZITOUN
  • 644
  • 1
  • 8
  • 23
0

@Houssem Zitoun's answer was good, and I'm marking it as the answer because it's relevant to my question. It's not the answer I used however

I already had the query written in sql that I wanted to use, so i decided to use a custom query with doctrine. It explains here how to do it. Symfony2 & Doctrine: Create custom SQL-Query

chrislebaron
  • 269
  • 3
  • 14