Search code examples
phpyiiinternal-server-error

Internal Server Error (500) with Yii returning large data sets


I have a Yii web service actionQuery that queries a model based on four parameters. There are about 1700 items to be queried total and I'm using a pretty bad web host (iPage). When I run a query with no parameters or extremely common parameters like "a" in string name, I expect to see all or almost all of the rows. Instead I get back a 500 Internal server error that is obviously not being produced by Yii, so it's a pretty bad error. When I try to narrow it down to around 700 or 800 rows, it takes a while but it gets done. How can I correct this error of large data sets producing 500 internal server errors? Is it a max execution time issue? Is there something I need to be doing differently with CDBCriteria?

Here is actionQuery, which is admittedly coded quite poorly.

 public function actionQuery()
    {
                $this->_checkAuth();

        switch ($_GET['model'])
                {
                        case 'dogFood':
                                $criteria = new CDbCriteria();
                                if ($_GET['name'] && $_GET['name'] !== '0') {
                                        $criteria->addSearchCondition('name_df', $_GET['name']);
                                }
                                if ($_GET['ingredients'] && $_GET['ingredients'] !== '0') {
                                        $ingredientsArray = explode(',',$_GET['ingredients']);
                                        foreach ($ingredientsArray as $ingredient) {
                                                $criteria->addSearchCondition('ingredients_df', $ingredient);
                                        }
                                }
                                if ($_GET['brand'] && $_GET['brand'] != 0) {
                                        $criteria->addColumnCondition(array('brand_df' => $_GET['brand']));
                                }
                                if ($_GET['brandstring'] && $_GET['brandstring'] !== 0) {
                                        $criteriaForBrand = new CDbCriteria();
                                        $criteriaForBrand->addSearchCondition('name_dfb', $_GET['brandstring']);
                                        $brandInQuestion = DogfoodbrandDfb::model()->find($criteriaForBrand);
                                        $brandId = $brandInQuestion->id_dfb;
                                        $criteria->addColumnCondition(array('brand_df' => $brandId));
                                }
                                $models = DogfoodDf::model()->findAll($criteria);
                                break;
                        default:
                                $this->_sendResponse(501, sprintf(
                                        'Error: Mode <b>query</b> is not implemented for model <b>%s</b>',
                                        $_GET['model']));
                                exit;
                }

                if (empty($models)) {
                        $this->_sendResponse(200,
                                sprintf('No items were found for model <b>%s</b>', $_GET['model']));
                }
                else {
                        $rows = array();

                        foreach ($models as $model) {
                                $rows[] = $model->attributes;
                        }

                        $this->_sendResponse(200, CJSON::encode($rows));
                }
    }

Solution

  • Most likely you are running out of memory. There is a limit set how much memory a PHP program can use: you should see memory exceeded messages in your error.log.

    You can try and up the allowed memory for a thread, or for this specific script, but verify in your error log first that this is the issue.