我有一个名为DepartementsController的控制器,它扩展了yii \ rest \ ActiveController。 我必须知道我所在地区的哪个部门。 当我测试我的行动时http://localhost/yii_2/ws_localisation/web/regions/01/departements 我有这个错误:
{
"name": "PHP Notice",
"message": "Trying to get property of non-object",
"code": 8,
"type": "yii\base\ErrorException",
"file": "E:\wamp\www\yii_2\ws_localisation\controllers\DepartementsController.php",
"line": 58,
"stack-trace": [
"#0 E:\wamp\www\yii_2\ws_localisation\controllers\DepartementsController.php(58): yii\base\ErrorHandler->handleError(8, 'Trying to get p...', 'E:\\wam...', 58, Array)",
"#1 [internal function]: app\controllers\DepartementsController->actionDepartements('01')",
"#2 E:\Laetitia\wamp\www\yii_2\ws_localisation\vendor\yiisoft\yii2\base\InlineAction.php(55): call_user_func_array(Array, Array)",
"#3 E:\Laetitia\wamp\www\yii_2\ws_localisation\vendor\yiisoft\yii2\base\Controller.php(154): yii\base\InlineAction->runWithParams(Array)",
"#4 E:\Laetitia\wamp\www\yii_2\ws_localisation\vendor\yiisoft\yii2\base\Module.php(454): yii\base\Controller->runAction('departements', Array)",
"#5 E:\Laetitia\wamp\www\yii_2\ws_localisation\vendor\yiisoft\yii2\web\Application.php(84): yii\base\Module->runAction('departements/de...', Array)",
"#6 E:\Laetitia\wamp\www\yii_2\ws_localisation\vendor\yiisoft\yii2\base\Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))",
"#7 E:\Laetitia\wamp\www\yii_2\ws_localisation\web\index.php(12): yii\base\Application->run()",
"#8 {main}"
]
}
你能帮帮我吗?
问候。
web.php
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['departements'],
'prefix' => '/regions/<id:\\w+>',
'patterns' => [
'GET,HEAD' => 'departements',
],
],
[
'class' => 'yii\rest\UrlRule',
'controller' => ['regions','departements','communes','sylvoecoregions'],
'tokens' => ['{id}' => '<id:\\w+>'],
'except' => ['create','update','delete'],
],
],
],
DepartementsController.php
namespace app\controllers;
use yii\rest\ActiveController;
use yii\db\ActiveRecord;
use yii\web\ServerErrorHttpException;
class DepartementsController extends ActiveController
{
public $modelClass = 'app\models\Departements';
public function behaviors()
{
return
\yii\helpers\ArrayHelper::merge(parent::behaviors(), [
'corsFilter' => [
'class' => \yii\filters\Cors::className(),
],
]);
}
public function actionDepartements($id)
{
$model = new $this->modelClass;
$model_departement = $model->find()->where('region_id=:id',[':id'=>$id])->all();
if($model_departement)
{
//$this->setHeader(200);
echo json_encode(array('status'=>1,'data'=>array_filter($model_departement->attributes)),JSON_PRETTY_PRINT);
}
else
{
//$this->setHeader(400);
echo json_encode(array('status'=>0,'error_code'=>400,'errors'=>$model->errors),JSON_PRETTY_PRINT);
}
}}
答案 0 :(得分:0)
如果您知道$model_departement
是一个实例
只需将您的if条件更改为
`if ($model_departement instanceof ModelDepartmentClass) {`
但如果你对此不了解。下面的行也有效。
if (is_object($model_departement)) {
if ($model_departement)
不是合格的支票。
请查看相关文件以获取更多信息。
http://php.net/manual/tr/function.is-object.php