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.

1 Answer 1


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?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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