<p>The <code>*</code> wildcard character should do the trick.</p>

<p>c.f. <a href="https://craftcms.com/docs/templating/elementcriteriamodel#parameter-value-syntax" rel="nofollow">https://craftcms.com/docs/templating/elementcriteriamodel#parameter-value-syntax</a></p>

<pre><code>$assets = craft()-&gt;elements-&gt;getCriteria(ElementType::Asset, array(
    'filename' =&gt; $prefix.'*',
    'limit' =&gt; null,
))-&gt;find();
</code></pre>

CC BY-SA 3.0
2016-09-01T22:43:09.563Z
Works like a charm. Thanks Michael! (I edited in a working example)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-09-01T22:39:43.547Z
2016-09-01T22:42:31.363Z
2016-09-01T22:42:31.363Z
Michael Rog
<p>A possible workaround is to use the <code>search</code> parameter like this:</p>

<pre><code>$assets = craft()-&gt;elements-&gt;getCriteria(ElementType::Asset, array(
    'search' =&gt; 'filename::'.$prefix.'*',
    'limit' =&gt; null,
))-&gt;find();
</code></pre>

CC BY-SA 3.0
2016-09-01T22:35:54.787Z
2016-09-01T22:35:54.787Z
Mats Mikkel Rummelhoff
<p>In a plugin, I'd like to pull all Assets with filenames starting with a certain string (the <code>$prefix</code> variable, in the below examples).</p>

<p>It's possible to do this with a custom DbCommand:</p>

<pre><code>$results = craft()-&gt;db-&gt;createCommand()
    -&gt;select('assetfiles.*')
    -&gt;from('assetfiles assetfiles')
    -&gt;where(array(
        'like',
        'assetfiles.filename',
        $prefix.'%',
    ))
    -&gt;queryAll();
$assets = AssetFileModel::populateModels($results);
</code></pre>

<p>However, I'd like to do this using an ElementCriteriaModel. The below doesn't work:</p>

<pre><code>$assets = craft()-&gt;elements-&gt;getCriteria(ElementType::Asset, array(
    'filename' =&gt; 'LIKE "'.$prefix.'%"',
    'limit' =&gt; null,
))-&gt;find();
</code></pre>

<p>In the above example, is the syntax off, or is not possible to use LIKE in an ElementCriteriaModel parameter?</p>

<plugin-development><elementcriteriamodel>
Is it possible to use the MySQL LIKE directive in an ElementCriteriaModel?
# Is it possible to use the MySQL LIKE directive in an ElementCriteriaModel?

(Asked by: username_0 on 2016-09-01)

In a plugin, I'd like to pull all Assets with filenames starting with a certain string (the $prefix variable, in the below examples).
It's possible to do this with a custom DbCommand:
$results = craft()->db->createCommand()
    ->select('assetfiles.*')
    ->from('assetfiles assetfiles')
    ->where(array(
        'like',
        'assetfiles.filename',
        $prefix.'%',
    ))
    ->queryAll();
$assets = AssetFileModel::populateModels($results);

However, I'd like to do this using an ElementCriteriaModel. The below doesn't work:
$assets = craft()->elements->getCriteria(ElementType::Asset, array(
    'filename' => 'LIKE "'.$prefix.'%"',
    'limit' => null,
))->find();

In the above example, is the syntax off, or is not possible to use LIKE in an ElementCriteriaModel parameter?

**Tags:** plugin-development, elementcriteriamodel


---

## 2 answers

### Answer by username_1 on 2016-09-01. Score: 2 | Accepted answer

The * wildcard character should do the trick.
c.f. https://craftcms.com/docs/templating/elementcriteriamodel#parameter-value-syntax
$assets = craft()->elements->getCriteria(ElementType::Asset, array(
    'filename' => $prefix.'*',
    'limit' => null,
))->find();

> **Comments:**
> **username_0**: Works like a charm. Thanks Michael! (I edited in a working example)  

---

### Answer by username_0 on 2016-09-01. Score: 1

A possible workaround is to use the search parameter like this:
$assets = craft()->elements->getCriteria(ElementType::Asset, array(
    'search' => 'filename::'.$prefix.'*',
    'limit' => null,
))->find();


---

