Foreach through a large table using Yii ActiveRecord - “out of memory” errors

纵饮孤独 提交于 2020-01-05 04:00:51

问题


I have a website on Yii Framework and I want to search a table for matching words.

I keep getting "out of memory" (it is a large table).

I try this code but it keeps loading the page

$dataProvider = new CActiveDataProvider('Data');
$iterator = new CDataProviderIterator($dataProvider);
foreach($iterator as $data) {
    echo $data->name."\n";
}

So I try this code but it keeps limiting the result to 10:

$dataProvider = new CActiveDataProvider('Data');
$iterator = new CDataProviderIterator($dataProvider);
foreach($dataProvider as $data) {
    echo $data->name."\n";
}

and if I do this I get the "out of memory" message:

$dataProvider = new CActiveDataProvider('Data' array(
    'criteria'=>array(
        'order'=>'id DESC',
    ),
    'pagination' => false
));

foreach($dataProvider as $data) {
    echo $data->name."\n";
}

回答1:


I don't know why are you need to load all search results in one page, but you can change number of items per page to desire value by this code (and using pagination):

$dataProvider = new CActiveDataProvider('Data' array(
    'criteria'=>array(
        'order'=>'id DESC',
        'condition' => 'town LIKE :search_town AND FK_country > :country_id',
        'params' => array(':search_town' => $search_town.'%', ':country_id' => 10)
    ),
     'pagination' => array(
         "pageSize" => 100,
         //"currentPage" => 0, //using for pagination
     )
));
$iterator = new CDataProviderIterator($dataProvider);
foreach($iterator as $data) {
     echo $data->name."\n";
}

Here http://sonsonz.wordpress.com/2011/10/14/yii-examples-of-using-cdbcriteria/ more examples




回答2:


It is unwise to use CActiveDataProvider for big datasets. Especially if you only want to perform background tasks on them. It would be advised to use direct SQL and go from there.

Based on the comments on CreatoR's answer, you are trying to find a number of occurences in a big table. As an example:

$connection=Yii::app()->db;
$sql = "SELECT id FROM data WHERE field1 LIKE '%someValue%' OR field2 LIKE '%someValue%' OR field3 LIKE '%someValue%'";
$command=$connection->createCommand($sql);
$numberOfRestuls=$command->execute();
//if you also want to display the results :
$ids=$command->queryAll();
$criteria=new CDbCriteria;
$criteria->addInCondition('id',$ids,'OR');
$dataProvider = new CActiveDataProvider('Data', $criteria);
//etc


来源:https://stackoverflow.com/questions/19973624/foreach-through-a-large-table-using-yii-activerecord-out-of-memory-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!