Is there a way to query if array field contains a certain value in Doctrine2?

Posted by dpimka on Stack Overflow See other posts from Stack Overflow or by dpimka
Published on 2012-10-29T16:58:35Z Indexed on 2012/10/29 17:00 UTC
Read the original article Hit count: 194

Filed under:
|
|
|

Starting out with Symfony2 + Doctrine.

I have a table with User objects (fos_user), for which my schema contains a roles column of an 'array' type.

Doctrine saves fields of this type by serializing them from php 'array' to 'longtext' (in mysql's case).

So let's say I have the following users saved into DB:

**User1**: array(ROLE_ADMIN, ROLE_CUSTOM1)
**User2**: array(ROLE_ADMIN, ROLE_CUSTOM2)
**User3**: array(ROLE_CUSTOM2)

Now in my controller I want to select all users with ROLE_ADMIN set. Is there a way to write a DQL query which would directly return me User1 and User2? Or do I need to fetch all users to have Doctrine to unserialize roles column and then for each of them do in_array('ROLE_ADMIN', $user->getRoles())?

I have searched the DQL part of the manual, but so far did not find anything similar to my needs...

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql