4

I am trying to export data from a database to a csv file, I am using the code below to retrieve the data from the DB, but it only returns 100 rows, how can I fix that?

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'educators';
$query = craft()->elements->buildElementsQuery($criteria);
$query->order('title asc');
$queryResults = $query->queryAll();
$educators = EntryModel::populateModels($queryResults);

foreach ($educators as $educator) {
   //Write to file
}
  • 2
    Your code could be simplified by adding an array as a second parameter to getCriteria(), i.e. $criteria = craft()->elements->getCriteria(ElementType::Entry, ['section' => 'educators', 'order' => 'title asc', 'limit' => null]); and then just $educators = $criteria->find(); – in other words; you don't need the calls to buildElementsQuery() and queryAll() in this particular instance, and $criteria->find() will actually return an array of EntryModels so there's no need to manually populate those. – Mats Mikkel Rummelhoff Jul 27 '16 at 7:13
10

Craft imposes a default limit of 100 for element queries. To disable this you need to specify a limit of null.

$criteria->limit = null;
  • $criteria->limit = -1 also works :) null is more clear, though. – Mats Mikkel Rummelhoff Jul 27 '16 at 7:16
5

By default, Craft sets a limit of 100. Set it to null: limit(null).

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.