Yii2 findOne()超过执行时间
我有一个操作,它执行一个简单的findOne($id)查询并从数据库返回一行。这超过了最大执行时间。此方法由多个类继承,在这些类中,is工作得非常好。我没有覆盖相关模型中的任何Yii2 findOne()超过执行时间,yii2,yii2-advanced-app,Yii2,Yii2 Advanced App,我有一个操作,它执行一个简单的findOne($id)查询并从数据库返回一行。这超过了最大执行时间。此方法由多个类继承,在这些类中,is工作得非常好。我没有覆盖相关模型中的任何find()或afterFind()方法 public function actionGetone($id) { $classname = $this->model; $model = new $classname; return $model::find
find()
或afterFind()
方法
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;
}
但只要我将其更改为returnreturn$items[0]代码>id再次死亡,并出现相同的错误
不确定是否链接了该操作,但如果在行为()
方法中未提及该操作,并且将其添加到访问规则(如下面所示)中,我将得到一个超过最大执行时间30秒的错误。但当我将访问角色更改为['*']时,它会在null上调用成员函数checkAccess()
错误。我没有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
}
发现问题,这与模型的toArray()
方法中的递归标志有关。在我的例子中,用户
具有作业
,而作业
具有在模型的字段()中指定的用户
。这将导致一个无限循环
将此添加到关系的模型中以避免无限循环:
public function toArray(array $fields = [], array $expand = [], $recursive = true)
{
return parent::toArray($fields, $expand, false);
}
id
是主键吗?如果没有,是否编制了索引?你们有几排?如果您打印出SQL并将其直接粘贴到数据库中,它运行正常吗?在使用->all()
之前,您是否尝试过使用print\r
打印查询?您可以使用以下命令打印activeQuery:$query->createCommand()->rawSql;我的数据库中只有28行<代码>变量转储($model->primaryKey())
给我[0=>'id']
尝试删除所有行为并检查它是否有效,如果它确实在您的某个行为中存在错误,请找出哪个查询正常工作,并且只返回一个预期结果。为什么抓取数组的第一项会导致超出执行时间?