5

I'm using Symfony2 and the Doctrine query builder. Is there an easy way to reverse a Boolean in the database?

I've tried this with no luck:

    $query->update('AppMonitorBundle:Monitor', 'm')
            ->set('m.isActive', '!m.isActive')
            ->where('m.id = :monitor')
            ->setParameter('monitor', $monitor)
            ->getQuery()
            ->execute()
    ;

Which I believe would work in SQL but it gives me:

[Syntax Error] line 0, col 51: Error: Expected Literal, got '!'

Substituting ! for NOT gives the same result.

James
  • 741
  • 1
  • 11
  • 28

1 Answers1

9

Here is a workaround:

->set('m.isActive', '1-m.isActive')

Tested and worked for me.

A boolean is just a 0 (as false) and 1 (as true). So if m.isActive is true, then the reverse value will false (1-1 = 0 = false). And if m.isActive is false, the reverse side will true (1-0 = 1 = true).

Thierry
  • 746
  • 13
  • 15