James
James

Reputation: 771

Reverse Boolean value in Doctrine

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.

Upvotes: 5

Views: 2202

Answers (1)

Thierry
Thierry

Reputation: 756

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).

Upvotes: 9

Related Questions