Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple IF test statement in Doctrine

Tags:

mysql

doctrine

Does Doctrine support IF statements? I get the following error:

Expected known function, got 'IF'

while executing this query with IF:

$qb->select("c.id, IF(c.type_id LIKE 9, c.name, c.lastname) as name")

It works fine while re-written in pure SQL. Any workarounds?

like image 359
Shepherd Avatar asked Apr 22 '15 09:04

Shepherd


People also ask

Is it possible to use if statements in doctrine 2?

E.g. an excellent solution adding almost all necessary ( not supported from box ) stuff for Doctrine 2 is DoctrineExtensions by beberlei (github). With it it's possible to use directly IF -statement like in OP's case: $qb->select ("..IF (condition, true-state, false-state)...")

What is a basic doctrine?

Basic doctrines are, rather, those which are fundamental to the Christian Way of Life and spiritual growth and which are prerequisites to the study of advanced concepts of the Bible. Note: there is no answer key for this "test".

How many questions are there in the Christian doctrine test?

The following are 20 basic Christian Doctrine questions. All you need to do is write down the answers to the questions, and when you are done, click on the Answers Page at the end of this test and find out how you did. Of course, there are a lot more questions that could be asked, but these will serve as a sample.

What's the best way to add additional functionalities to doctrine 2?

Think it's better to use some extra functional in such cases ( without trying "to circumvent" theirs). E.g. an excellent solution adding almost all necessary ( not supported from box ) stuff for Doctrine 2 is DoctrineExtensions by beberlei (github).


2 Answers

Yes if statements in doctrine is not supported you may convert it to case when

IF(c.type_id LIKE 9, c.name, c.lastname) as name

to

case when c.type_id = 9 then c.name else c.lastname end as name

UPDATE: From the comment does concat function is allowed in case-when

The answer is yes very much allowed. Here is an example

mysql> select * from timesheets ;
+-----------+-------+----------+
| client_id | hours | category |
+-----------+-------+----------+
|         1 |  1.50 | onsite   |
|         1 |  1.50 | onsite   |
|         1 |  1.00 | remote   |
|         2 |  1.50 | remote   |
+-----------+-------+----------+
4 rows in set (0.00 sec)

mysql> select 
case when category = 'onsite' then concat('ON',' ',hours) else hours
end as dd from timesheets ;
+---------+
| dd      |
+---------+
| ON 1.50 |
| ON 1.50 |
| 1.00    |
| 1.50    |
+---------+
4 rows in set (0.00 sec)
like image 115
Abhik Chakraborty Avatar answered Oct 08 '22 00:10

Abhik Chakraborty


Note: for Doctrine 2

Think it's better to use some extra functional in such cases ( without trying "to circumvent" theirs). E.g. an excellent solution adding almost all necessary ( not supported from box ) stuff for Doctrine 2 is DoctrineExtensions by beberlei (github). With it it's possible to use directly IF-statement like in OP's case:

("Symfony-example") E.g. in your config.xml add lines:

orm:
    ..
    entity_managers:
            ....
            dql:
                ....
                string_functions:
                    IF: DoctrineExtensions\Query\Mysql\IfElse

Then U can use it anywhere like:

 $qb->select("..IF(condition, true-state, false-state)...")
like image 42
voodoo417 Avatar answered Oct 08 '22 00:10

voodoo417