<p>I think you want to have the same result as with an ElementCriteriaModel, instead of getting an ElementCriteriaModel.</p>

<p>What you want to do is mimic <code>craft()-&gt;elements-&gt;findElements()</code>, and put your own <code>$query</code> in it. This will more or less put the results in their models. You can just copy this function and replace the second line (where <code>$query</code> is defined) with your logics.</p>

<p>You could also do something like <code>Conference_SessionModel::populateModels($queryResult)</code>, but by the way Craft joins the content table in findElements this would be rather slow, as it would make a new query for every content field.</p>

<p>What would be really nice is if you could specify <code>$query</code> in findElements, too bad Craft doesn't support this (yet?)</p>

CC BY-SA 3.0
2015-06-10T09:20:07.480Z
2015-06-10T18:48:09.357Z
2015-06-10T18:48:09.357Z
Bob Olde Hampsink
<p>Is it possible to turn a query into an element criteria model? Currently I'm doing this:</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria('Conference_Session');
$criteria-&gt;conferenceId = $conferenceModel-&gt;id;

$query = craft()-&gt;elements-&gt;buildElementsQuery($criteria);
$query-&gt;order("conference_session.cs_start_time ASC");

$queryResult = $query-&gt;queryAll();
</code></pre>

<p>I would prefer to be able to be able to add my MySQL commands then turn it back into a element criteria. I don't <strong>have</strong> to do this but I want to know if I could.</p>

<elementcriteriamodel><queries>
Query to element criteria
# Query to element criteria

(Asked by: username_0 on 2015-05-13)

Is it possible to turn a query into an element criteria model? Currently I'm doing this:
$criteria = craft()->elements->getCriteria('Conference_Session');
$criteria->conferenceId = $conferenceModel->id;

$query = craft()->elements->buildElementsQuery($criteria);
$query->order("conference_session.cs_start_time ASC");

$queryResult = $query->queryAll();

I would prefer to be able to be able to add my MySQL commands then turn it back into a element criteria. I don't have to do this but I want to know if I could.

**Tags:** elementcriteriamodel, queries


---

## 1 answers

### Answer by username_1 on 2015-06-10. Score: 2

I think you want to have the same result as with an ElementCriteriaModel, instead of getting an ElementCriteriaModel.

What you want to do is mimic `craft()->elements->findElements()`, and put your own `$query` in it. This will more or less put the results in their models. You can just copy this function and replace the second line (where `$query` is defined) with your logics.

You could also do something like `Conference_SessionModel::populateModels($queryResult)`, but by the way Craft joins the content table in findElements this would be rather slow, as it would make a new query for every content field.

What would be really nice is if you could specify `$query` in findElements, too bad Craft doesn't support this (yet?)


---

