<p>When you do this:</p>



<pre class="lang-php prettyprint-override"><code>$criteria-&gt;accent = '';
</code></pre>

<p>Craft is going to look for users where the <code>accent</code> field’s DB cell is an empty string.</p>

<p>Those values are probably <code>null</code> though, if they aren’t getting returned.</p>

<p>Try setting your <code>accent</code> param to <code>:empty:</code> instead of <code>''</code>. When you do that, Craft will search for both empty strings <em>and</em> <code>null</code> values, so you should get the results you’re expecting.</p>

CC BY-SA 3.0
2015-10-11T12:26:25.093Z
OK thx now I see the number lines for empty strings and `null` values is correct : `$criteria->accent = ':empty:';`  > 1306 USERS with empty/NULL accent. 
But there is still a strange thing with the value zero : it gives 3109 users, so the total number users.
migmaker
CC BY-SA 3.0
2015-10-11T12:28:18.673Z
Try changing `$criteria->accent = $i;` to `$criteria->accent = (string)$i;`
Brandon Kelly
CC BY-SA 3.0
2015-10-11T14:31:55.110Z
Not working. Still give me the total number of users. I tried also to provide it specifically : `$criteria->accent = '0';` but same thing (total number of users = 3109). Really bizarre...
migmaker
CC BY-SA 3.0
2015-10-11T10:38:53.523Z
2015-10-11T10:38:53.523Z
Brandon Kelly
<p>I made a plugin which count the number of users per groups defined by the field <code>accent</code> which is a text field and can be <strong>empty</strong> or contain <strong>numbers</strong> from 0 to 12.</p>

<pre><code>public function actionCountUsers() {

    // Delete all the entry caches
    craft()-&gt;templateCache-&gt;deleteCachesByElementType('User');

    // Now find the entry IDs
    $criteria = craft()-&gt;elements-&gt;getCriteria('User', array('group' =&gt; 'unepMembers', 'limit' =&gt; null, 'status' =&gt; null, 'localeEnabled' =&gt; null));
    $criteria-&gt;group = 'unepMembers';
    $criteria-&gt;limit = null;
    $criteria-&gt;status = null;
    $criteria-&gt;localeEnabled = null;

    //
    $totalEntries = $criteria-&gt;total();
    UnepPlugin::log($totalEntries.' USERS (no criteria)', LogLevel::Warning);

    //
    $criteria-&gt;accent = '';
    $totalEntries = $criteria-&gt;total();
    UnepPlugin::log($totalEntries.' USERS with empty accent', LogLevel::Warning);

    //
    $totalUsers = 0;
    for ($i = 0; $i &lt;= 15; $i++) {
        $criteria-&gt;accent = $i;
        $totalEntries = $criteria-&gt;total();
        MigPlugin::log($totalEntries.' USERS with accent :'.$criteria-&gt;accent, LogLevel::Warning);

        $totalUsers = $totalUsers + $totalEntries;
    }
    MigPlugin::log($totalUsers.' USERS with accents', LogLevel::Warning);

}
</code></pre>

<p>As you can see the result here below there is a problem with a criteria values <strong>empty</strong> or <strong>0</strong> on the <code>accent</code> field: they just do not work and count the total number of users.</p>

<pre><code>***********************
2015/10/11 07:09:48 [warning] [plugin] 3109 USERS (no criteria)  
2015/10/11 07:09:48 [warning] [plugin] 3109 USERS with empty accent  
2015/10/11 07:14:01 [warning] [plugin] 3109 USERS with accent :0  
2015/10/11 07:09:48 [warning] [plugin] 1849 USERS with accent :1  
2015/10/11 07:09:48 [warning] [plugin] 501 USERS with accent :2  
2015/10/11 07:09:48 [warning] [plugin] 0 USERS with accent :3  
2015/10/11 07:09:48 [warning] [plugin] 72 USERS with accent :4  
2015/10/11 07:09:49 [warning] [plugin] 8 USERS with accent :5  
2015/10/11 07:09:49 [warning] [plugin] 1 USERS with accent :6  
2015/10/11 07:09:49 [warning] [plugin] 0 USERS with accent :7  
2015/10/11 07:09:49 [warning] [plugin] 4 USERS with accent :8  
2015/10/11 07:09:49 [warning] [plugin] 81 USERS with accent :9  
2015/10/11 07:09:49 [warning] [plugin] 232 USERS with accent :10  
2015/10/11 07:09:49 [warning] [plugin] 246 USERS with accent :11  
2015/10/11 07:09:49 [warning] [plugin] 115 USERS with accent :12  
2015/10/11 07:09:49 [warning] [plugin] 0 USERS with accent :13  
2015/10/11 07:09:49 [warning] [plugin] 0 USERS with accent :14  
2015/10/11 07:09:49 [warning] [plugin] 0 USERS with accent :15  
2015/10/11 07:09:49 [warning] [plugin] 3109 USERS with accents  
</code></pre>

<p>Craft bug or did I miss something in my code?</p>

<plugin-development><elementcriteriamodel>
Plugins with element criteria selection does not work for empty or zero values
# Plugins with element criteria selection does not work for empty or zero values

(Asked by: username_0 on 2015-10-11)

I made a plugin which count the number of users per groups defined by the field accent which is a text field and can be empty or contain numbers from 0 to 12.
public function actionCountUsers() {

    // Delete all the entry caches
    craft()->templateCache->deleteCachesByElementType('User');

    // Now find the entry IDs
    $criteria = craft()->elements->getCriteria('User', array('group' => 'unepMembers', 'limit' => null, 'status' => null, 'localeEnabled' => null));
    $criteria->group = 'unepMembers';
    $criteria->limit = null;
    $criteria->status = null;
    $criteria->localeEnabled = null;

    //
    $totalEntries = $criteria->total();
    UnepPlugin::log($totalEntries.' USERS (no criteria)', LogLevel::Warning);

    //
    $criteria->accent = '';
    $totalEntries = $criteria->total();
    UnepPlugin::log($totalEntries.' USERS with empty accent', LogLevel::Warning);

    //
    $totalUsers = 0;
    for ($i = 0; $i <= 15; $i++) {
        $criteria->accent = $i;
        $totalEntries = $criteria->total();
        MigPlugin::log($totalEntries.' USERS with accent :'.$criteria->accent, LogLevel::Warning);

        $totalUsers = $totalUsers + $totalEntries;
    }
    MigPlugin::log($totalUsers.' USERS with accents', LogLevel::Warning);

}

As you can see the result here below there is a problem with a criteria values empty or 0 on the accent field: they just do not work and count the total number of users.
***********************
2015/10/11 07:09:48 [warning] [plugin] 3109 USERS (no criteria)  
2015/10/11 07:09:48 [warning] [plugin] 3109 USERS with empty accent  
2015/10/11 07:14:01 [warning] [plugin] 3109 USERS with accent :0  
2015/10/11 07:09:48 [warning] [plugin] 1849 USERS with accent :1  
2015/10/11 07:09:48 [warning] [plugin] 501 USERS with accent :2  
2015/10/11 07:09:48 [warning] [plugin] 0 USERS with accent :3  
2015/10/11 07:09:48 [warning] [plugin] 72 USERS with accent :4  
2015/10/11 07:09:49 [warning] [plugin] 8 USERS with accent :5  
2015/10/11 07:09:49 [warning] [plugin] 1 USERS with accent :6  
2015/10/11 07:09:49 [warning] [plugin] 0 USERS with accent :7  
2015/10/11 07:09:49 [warning] [plugin] 4 USERS with accent :8  
2015/10/11 07:09:49 [warning] [plugin] 81 USERS with accent :9  
2015/10/11 07:09:49 [warning] [plugin] 232 USERS with accent :10  
2015/10/11 07:09:49 [warning] [plugin] 246 USERS with accent :11  
2015/10/11 07:09:49 [warning] [plugin] 115 USERS with accent :12  
2015/10/11 07:09:49 [warning] [plugin] 0 USERS with accent :13  
2015/10/11 07:09:49 [warning] [plugin] 0 USERS with accent :14  
2015/10/11 07:09:49 [warning] [plugin] 0 USERS with accent :15  
2015/10/11 07:09:49 [warning] [plugin] 3109 USERS with accents  

Craft bug or did I miss something in my code?

**Tags:** plugin-development, elementcriteriamodel

---

## 1 answers

### Answer by username_1 on 2015-10-11. Score: 3

When you do this:
$criteria->accent = '';

Craft is going to look for users where the accent field’s DB cell is an empty string.
Those values are probably null though, if they aren’t getting returned.
Try setting your accent param to :empty: instead of ''. When you do that, Craft will search for both empty strings and null values, so you should get the results you’re expecting.

> **Comments:**
> **username_0**: OK thx now I see the number lines for empty strings and `null` values is correct : `$criteria->accent = ':empty:';`  > 1306 USERS with empty/NULL accent.  But there is still a strange thing with the value zero : it gives 3109 users, so the total number users.  
> **username_1**: Try changing `$criteria->accent = $i;` to `$criteria->accent = (string)$i;`  
> **username_0**: Not working. Still give me the total number of users. I tried also to provide it specifically : `$criteria->accent = '0';` but same thing (total number of users = 3109). Really bizarre...  

---

