Yii2 在`$this->modelClass::find()->andWhere(['post_id' => \Yii::$app->request->get('postId')]) 上出现语法错误` 这是一个有效的子句
Yii2 Getting a Syntax error on ` $this->modelClass::find()->andWhere(['post_id' => \Yii::$app->request->get('postId')])` which is a valid clause
我收到此错误:“语法错误,意外的‘::’ (T_PAAMAYIM_NEKUDOTAYIM),需要‘]’”
在有效条款上。
<?php namespace app\controllers;
use app\models\Comment;
use yii\data\ActiveDataProvider;
use yii\rest\ActiveController;
class CommentController extends ActiveController{
public $modelClass = Comment::class;
public function actions()
{
$actions = parent::actions(); //Auto generated STUD
$actions['index']['prepareDataProvider'] = [$this,'prepareDataProvider']; // Replace for custom
return $actions;
}
public function prepareDataProvider()
{
return new ActiveDataProvider([
"query" => $this->modelClass::find()->andWhere(['post_id' => \Yii::$app->request->get('postId')])
]);
}
}
如果我将“$this->modelClass”替换为“Comment”或任何模型名称,它就可以正常工作。
知道为什么会这样吗?
为此,我们使用 PHP 5.6.36,因为主应用程序是用它实现的。
设置一个变量并使用它。
public function prepareDataProvider()
{
$class = $this->modelClass;
return new ActiveDataProvider([
"query" => $class::find()->andWhere(['post_id' => \Yii::$app->request->get('postId')])
]);
}
我收到此错误:“语法错误,意外的‘::’ (T_PAAMAYIM_NEKUDOTAYIM),需要‘]’” 在有效条款上。
<?php namespace app\controllers;
use app\models\Comment;
use yii\data\ActiveDataProvider;
use yii\rest\ActiveController;
class CommentController extends ActiveController{
public $modelClass = Comment::class;
public function actions()
{
$actions = parent::actions(); //Auto generated STUD
$actions['index']['prepareDataProvider'] = [$this,'prepareDataProvider']; // Replace for custom
return $actions;
}
public function prepareDataProvider()
{
return new ActiveDataProvider([
"query" => $this->modelClass::find()->andWhere(['post_id' => \Yii::$app->request->get('postId')])
]);
}
}
如果我将“$this->modelClass”替换为“Comment”或任何模型名称,它就可以正常工作。 知道为什么会这样吗?
为此,我们使用 PHP 5.6.36,因为主应用程序是用它实现的。
设置一个变量并使用它。
public function prepareDataProvider()
{
$class = $this->modelClass;
return new ActiveDataProvider([
"query" => $class::find()->andWhere(['post_id' => \Yii::$app->request->get('postId')])
]);
}