YII queries not working [closed]

2019-03-05 13:34发布

I have a table with around 700,000 rows.

Profile::model()->findAll("country='US'")

I used this to find all rows that have 'US' as its country. But the execution stopped and I didn't get any result. But then I added limit like below

Profile::model()->findAll("country='US' limit 10000")

then it worked.

why the execution stopped? Please help me.. I'm new to YII.

标签: php yii
1条回答
成全新的幸福
2楼-- · 2019-03-05 13:45

If you use findAll, then all records will be returned without any limit applied. However, CActiveDataProvider automatically paginates results if you don't tell it otherwise. As mentioned in the comments, you are likely running out of memory when using findAll. Using a limit or pagination (which automatically applies a limit) lowers the returned number of rows to what your application can handle.

I recommend using CDataProviderIterator to fetch large numbers of active records. Try the following sample code.

$criteria = new CDbCriteria;
$criteria->compare('country','US');

$Profiles = CDataProviderIterator(
   new CActiveDataProvider(
     'Profile',
      array('criteria'=>$criteria)
   ),
   10000
 );

foreach ($Profiles as $Profile) {
  //do whatever
}

Using the iterator, Yii will automatically fetch just 10000 records (a number you say worked without running out of memory) at a time, and then remove them from memory before grabbing the next page. Conveniently all the records can be accessed with a foreach loop without any further work from you.

Change the 10000 value as necessary to achieve desired results. Also, make sure you are monitoring your error logs to identify out of memory and other errors.

查看更多
登录 后发表回答