2016-02-29 129 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); 
}