2016-02-29 124 views
0

我有一个操作正在执行一个简单的findOne($ id)查询并从数据库返回一行。这超出了最大执行时间。这个方法被多个类继承,其中的工作非常好。我并不是否定有关模型中的任何find()afterFind()方法。Yii2 findOne()超出执行时间

public function actionGetone($id) 
    { 
     $classname = $this->model; 
     $model = new $classname; 
     return $model::findOne($id); 
    } 

我没有得到任何错误,如果我覆盖的方法与按预期工作:

public function actionGetone($id){ 
     $items = Job::find()->where(['id' => $id])->all(); 
     return $items; 
    } 

,但只要我改变它返回return $items[0]; ID与同样超出错误再次死亡。

不确定这是否链接,但在behaviours()方法中未提及该操作时以及将其添加到访问规则(如下所示)时,我会收到Maximum execution time of 30 seconds exceeded错误。但是当我将访问角色更改为['*']时,它会给我一个Call to a member function checkAccess() on null错误。我没有安装authManager。

public function behaviors() 
{ 
    return [ 
     'contentNegotiator' => [ 
      'class' => \yii\filters\ContentNegotiator::className(), 
      'formats' => [ 
       'application/json' => yii\web\Response::FORMAT_JSON, 
      ], 
     ], 
     'authenticator' => [ 
      'class' => \yii\filters\auth\HttpBearerAuth::className(), 
      'only' => [ 'delete','patch','getone'], 
     ], 
     'access' => [ 
      'class' => \yii\filters\AccessControl::className(), 
      'only' => ['delete','patch','getone'], 
      'rules' => [ 
       [ 
        'actions' => ['delete','patch','getone'], 
        'allow' => true, 
        'roles' => ['@'], 
       ], 
      ], 
     ] 
    ]; 
} 

我会很感激的任何想法:)

更新

$items = Job::find()->where(['id' => $id]); 
return $items; 

给出:

{ 
    "sql": null, 
    "on": null, 
    "joinWith": null, 
    "select": null, 
    "selectOption": null, 
    "distinct": null, 
    "from": null, 
    "groupBy": null, 
    "join": null, 
    "having": null, 
    "union": null, 
    "params": [], 
    "where": { 
    "id": "3" 
    }, 
    "limit": null, 
    "offset": null, 
    "orderBy": null, 
    "indexBy": null, 
    "modelClass": "common\models\Job", 
    "with": null, 
    "asArray": null, 
    "multiple": null, 
    "primaryModel": null, 
    "link": null, 
    "via": null, 
    "inverseOf": null 
} 
+0

是'id'主键?如果没有,是否索引?你有多少行?如果你打印出SQL并将其直接粘贴到数据库中,它是否可以正常运行?在使用' - > all()'之前,您是否尝试用'print_r'打印出查询? – h2ooooooo

+0

您可以使用以下命令打印activeQuery:$ query-> createCommand() - > rawSql; – Ruben

+0

我的db中只有28行。 'var_dump($ model-> primaryKey());'给我'[0 =>'id']' – JPickup

回答

0

发现了问题,它的做用递归标志该模型的toArray()方法。在我的情况下,userjobsjobusers在模型的fields()方法中指定。这导致无限循环。

一下添加到关系的模式,以避免无限循环:

public function toArray(array $fields = [], array $expand = [], $recursive = true) 
{ 
    return parent::toArray($fields, $expand, false); 
}