0

This may have been answered before but my searching has not come up with anything. I need clarification on when to quote and when not to quote the boolean in the admin criteria for craft.users in a Twig template.

I have a plugin that displays a table of users and the reason this came up is I needed to exclude the admins from the table. Easy you say, just use the admin parameter and set it to false. That's what I thought so I did this:

{% set users = craft.users.admin(false) %}

This however returned all the users. So I thought maybe it needs quoting and did this:

{% set users = craft.users.admin('false') %}

Hurray! This did exactly what I wanted. Then I thought what if I only wanted the admins and tried:

{% set users = craft.users.admin('true') %}

This however returned all users that are not admins, eh? Ok, what about:

{% set users = craft.users.admin(true) %}

Hurray again! Only admins.

So looking through the source I can see why the first unquoted false has no affect as it fails the if(criteria->admin){} and therefore does nothing. After that I got lost in parseParam() and arrayHelpers so if anyone could clarify to quote or not to quote (that is the question!) and if this is the expected behaviour. Seems a little strange you need to quote 'false' and not quote true to get the desired behaviour. I also tried passing the parameters as an object and the results were the same.

2

Let me try to make some sense of the behavior you’re seeing.

First, you should know that the admin param will query against the admin column in your craft_users table, which will either be set to 0 or 1 for each user.

Like most Craft params, the admin param is set to null by default, and will only have any affect on the DB query if it is set to a non-empty value. (Here’s a list of what PHP considers “empty”: http://php.net/manual/en/function.empty.php#refsect1-function.empty-returnvalues)

So, here’s what’s happening behind the scenes for each thing you passed in:

  • false – That’s an empty value, so it’s the same as not passing anything in.
  • 'false' – Since the admin column is numeric, MySQL has to normalize this string into a number (sortof like using JavaScript’s parseInt() function). The result for 'false' is 0, since 'false' doesn’t begin with a number. The end result is that this awkwardly works as expected, but I wouldn’t recommend it.
  • 'true' – Same effect as 'false'. Ends up being 0.
  • true – SQL doesn’t support PHP’s boolean datatypes (true/false), so PHP will convert this to 1 before passing it off to MySQL. (false likewise becomes 0, but since false is empty, Craft won’t let your value make it that far in the first place.)

In the future, I think this makes a good case for us allowing false, so we’ll probably start checking for that and allowing it. But that won’t help you today.

So the question is then, what is the best way to find users that are(n’t) admins today? Here’s what I would recommend:

Finding admins:

  • 1 (int)
  • '1' (string)
  • true

Finding non-admins:

  • 'not 1' (string)
  • Lol, so I was doing it completely wrong but somehow managed to return the results by luck. I guess it would be good to have this documented as I couldn't find any examples and the only reference I found was on the Cheat Sheet over at Straight Up Craft with 'true or false' next to the 'admin' parameter. As the docs for craft.user didn't show an example I assumed it would behave like the fixedOrder parameter which you show a true or should this be a 1 as well? – Neal Hulme Oct 22 '14 at 19:44
  • Just updated the craft.users page to explicitly state that you should enter '1' or 'not 1', plus included a couple examples. fixedOrder is different, because that param doesn’t translate directly into a DB condition like admin does. – Brandon Kelly Oct 22 '14 at 20:50
  • Ah, that makes sense... is it good practice then to use 0's and 1's instead of true and false when dealing with DB queries to avoid these scenarios? A useful concept to be aware of. – Neal Hulme Oct 22 '14 at 21:03
  • Not 0s, because 0 and '0' are considered “empty” ;) – Brandon Kelly Oct 22 '14 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.