yii 为什么我得到一个错误致命错误:对非对象调用成员函数getUserOptions()

vc9ivgsu  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(128)

我正在学习Yii,我正在编写一个应用程序进行练习,但是当我调用actionupdate时,我得到了一个错误“Fatal error:对非对象”“调用成员函数getUserOptions().
这是我密码,你能告诉我为什么吗?
issuecontroller.php

public function actionUpdate($id)
    {
        $model=$this->loadModel($id);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Issue']))
        {

            $model->attributes=$_POST['Issue'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('update',array(
            'model'=>$model,
        ));
    }

...

public function loadModel($id)
{
    $model=Issue::model()->findByPk((int)$id);
    if($model===null)
        throw new CHttpException(404,'The requested page does not exist.');
    return $model;
}

...

public function getProject()
      {
        return $this->_project;
       } 

}

...
project.php

public function getUserOptions()
    {
         $usersArray=CHtml::listData($this->users,'id','username'); 

        return $usersArray;

}
_form.php

<?php echo $form->dropDownList($model,'owner_id', $this->getProject()->getUserOptions()); ?>

http://localhost/trackstar/index.php?r=issue/update&id=15
this is my error page但是,当我调用“actioncreate”时,没有错误。
我不知道为什么当我调用“issue/create&pid=1”时,我可以var_dump($this);如下所示,但为什么我调用“issue/update&id=1”,我得到的是“null”。
我调用了“issue/create&pid=1”,我可以var_dump($this);如下所示

object(IssueController)[10]
  private '_project' => 
    object(Project)[41]
      private '_md' (CActiveRecord) => 
        object(CActiveRecordMetaData)[23]
          public 'tableSchema' => 
            object(CMysqlTableSchema)[27]
              ...
          public 'columns' => 
            array (size=7)
              ...
          public 'relations' => 
            array (size=2)
              ...
          public 'attributeDefaults' => 
            array (size=0)
              ...
          private '_model' => 
            object(Project)[22]
              ...
      private '_new' (CActiveRecord) => boolean false
      private '_attributes' (CActiveRecord) => 
        array (size=7)
          'id' => string '1' (length=1)
          'name' => string 'project1' (length=8)
          'description' => null
          'create_time' => null
          'create_user_id' => null
          'update_time' => null
          'update_user_id' => null
      private '_related' (CActiveRecord) => 
        array (size=0)
          empty
      private '_c' (CActiveRecord) => null
      private '_pk' (CActiveRecord) => string '1' (length=1)
      private '_alias' (CActiveRecord) => string 't' (length=1)
      private '_errors' (CModel) => 
        array (size=0)
          empty
      private '_validators' (CModel) => null
      private '_scenario' (CModel) => string 'update' (length=6)
      private '_e' (CComponent) => null
      private '_m' (CComponent) => null
  public 'layout' => string '//layouts/column2' (length=17)
  public 'menu' => 
    array (size=2)
      0 => 
        array (size=2)
          'label' => string 'List Issue' (length=10)
          'url' => 
            array (size=1)
              ...
      1 => 
        array (size=2)
          'label' => string 'Manage Issue' (length=12)
          'url' => 
            array (size=1)
              ...
  public 'breadcrumbs' => 
    array (size=2)
      'Issues' => 
        array (size=1)
          0 => string 'index' (length=5)
      0 => string 'Create' (length=6)
  public 'defaultAction' => string 'index' (length=5)
  private '_id' (CController) => string 'issue' (length=5)
  private '_action' (CController) => 
    object(CInlineAction)[11]
      private '_id' (CAction) => string 'create' (length=6)
      private '_controller' (CAction) => 
        &object(IssueController)[10]
      private '_e' (CComponent) => null
      private '_m' (CComponent) => null
  private '_pageTitle' (CController) => null
  private '_cachingStack' (CController) => null
  private '_clips' (CController) => null
  private '_dynamicOutput' (CController) => null
  private '_pageStates' (CController) => null
  private '_module' (CController) => null
  private '_widgetStack' (CBaseController) => 
    array (size=1)
      0 => 
        object(CActiveForm)[68]
          public 'action' => string '' (length=0)
          public 'method' => string 'post' (length=4)
          public 'stateful' => boolean false
          public 'errorMessageCssClass' => string 'errorMessage' (length=12)
          public 'htmlOptions' => 
            array (size=1)
              ...
          public 'clientOptions' => 
            array (size=0)
              ...
          public 'enableAjaxValidation' => boolean false
          public 'focus' => null
          private '_attributes' => 
            array (size=0)
              ...
          private '_summary' => null
          public 'actionPrefix' => null
          public 'skin' => string 'default' (length=7)
          private '_id' (CWidget) => string 'issue-form' (length=10)
          private '_owner' (CWidget) => 
            &object(IssueController)[10]
          private '_widgetStack' (CBaseController) => 
            array (size=0)
              ...
          private '_e' (CComponent) => null
          private '_m' (CComponent) => null
  private '_e' (CComponent) => null
  private '_m' (CComponent) => null

我调用了“issue/update&id=1”,得到的结果是“null”。

object(IssueController)[10]
  private '_project' => null
  public 'layout' => string '//layouts/column2' (length=17)
  public 'menu' => 
    array (size=4)
      0 => 
        array (size=2)
          'label' => string 'List Issue' (length=10)
          'url' => 
            array (size=1)
              ...
      1 => 
        array (size=2)
          'label' => string 'Create Issue' (length=12)
          'url' => 
            array (size=1)
              ...
      2 => 
        array (size=2)
          'label' => string 'View Issue' (length=10)
          'url' => 
            array (size=2)
              ...
      3 => 
        array (size=2)
          'label' => string 'Manage Issue' (length=12)
          'url' => 
            array (size=1)
              ...
  public 'breadcrumbs' => 
    array (size=3)
      'Issues' => 
        array (size=1)
          0 => string 'index' (length=5)
      'issue belong to project1' => 
        array (size=2)
          0 => string 'view' (length=4)
          'id' => string '15' (length=2)
      0 => string 'Update' (length=6)
  public 'defaultAction' => string 'index' (length=5)
  private '_id' (CController) => string 'issue' (length=5)
  private '_action' (CController) => 
    object(CInlineAction)[11]
      private '_id' (CAction) => string 'update' (length=6)
      private '_controller' (CAction) => 
        &object(IssueController)[10]
      private '_e' (CComponent) => null
      private '_m' (CComponent) => null
  private '_pageTitle' (CController) => null
  private '_cachingStack' (CController) => null
  private '_clips' (CController) => null
  private '_dynamicOutput' (CController) => null
  private '_pageStates' (CController) => null
  private '_module' (CController) => null
  private '_widgetStack' (CBaseController) => 
    array (size=1)
      0 => 
        object(CActiveForm)[46]
          public 'action' => string '' (length=0)
          public 'method' => string 'post' (length=4)
          public 'stateful' => boolean false
          public 'errorMessageCssClass' => string 'errorMessage' (length=12)
          public 'htmlOptions' => 
            array (size=1)
              ...
          public 'clientOptions' => 
            array (size=0)
              ...
          public 'enableAjaxValidation' => boolean false
          public 'focus' => null
          private '_attributes' => 
            array (size=0)
              ...
          private '_summary' => null
          public 'actionPrefix' => null
          public 'skin' => string 'default' (length=7)
          private '_id' (CWidget) => string 'issue-form' (length=10)
          private '_owner' (CWidget) => 
            &object(IssueController)[10]
          private '_widgetStack' (CBaseController) => 
            array (size=0)
              ...
          private '_e' (CComponent) => null
          private '_m' (CComponent) => null
  private '_e' (CComponent) => null
  private '_m' (CComponent) => null

我怀疑问题出在我的issueController.php上,如下所示,你能告诉我是什么原因吗?

<?php

class IssueController extends Controller
{
    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */

    private $_project = null;
    public $layout='//layouts/column2';

    /**
     * @return array action filters
     */

    protected function loadProject($project_id) {
        //if the project property is null, create it based on input id
        if($this->_project===null)
        {
            $this->_project=Project::model()->findbyPk($project_id);

            if($this->_project===null)
            {
                throw new CHttpException(404,'The requested project does not
exist.');
            }
        }
        return $this->_project;
    }
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
            'projectContext + create',
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('index','view'),
                'users'=>array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('create','update'),
                'users'=>array('@'),
            ),
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions'=>array('admin','delete'),
                'users'=>array('admin'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionView($id)
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }

    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate()
    {
        $model=new Issue;
        $model->project_id = $this->_project->id;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Issue']))
        {
            $model->attributes=$_POST['Issue'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('create',array(
            'model'=>$model,
        ));
    }
    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        /* var_dump($model);
        exit; 
        */ //we can get the record of $id with $model 

        if(isset($_POST['Issue']))
        {

            $model->attributes=$_POST['Issue'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('update',array(
            'model'=>$model,
        ));
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete($id)
    {
        if(Yii::app()->request->isPostRequest)
        {
            // we only allow deletion via POST request
            $this->loadModel($id)->delete();

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if(!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
        else
            throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
    }

    /**
     * Lists all models.
     */
    public function actionIndex()
    {
        $dataProvider=new CActiveDataProvider('Issue');
        $this->render('index',array(
            'dataProvider'=>$dataProvider,
        ));
    }

    /**
     * Manages all models.
     */
    public function actionAdmin()
    {
        $model=new Issue('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Issue']))
            $model->attributes=$_GET['Issue'];

        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id)
    {
        $model=Issue::model()->findByPk((int)$id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='issue-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

    public function filterProjectContext($filterChain)
    {
        //set the project identifier based on either the GET or POST input
        //request variables, since we allow both types for our actions
        $projectId = null;
        if(isset($_GET['pid']))
            $projectId = $_GET['pid'];
            else
                if(isset($_POST['pid']))
                    $projectId = $_POST['pid'];
                    $this->loadProject($projectId);
                    //complete the running of other filters and execute the requested action
                    $filterChain->run();

}

    public function getProject()
      {
        return $this->_project;
       } 

}

当我调用“issue/update&id=15”时,我试图将“var_dump($model);“我得到了如下。

object(Issue)[48]
  private '_md' (CActiveRecord) => 
    object(CActiveRecordMetaData)[24]
      public 'tableSchema' => 
        object(CMysqlTableSchema)[28]
          public 'schemaName' => null
          public 'name' => string 'tbl_issue' (length=9)
          public 'rawName' => string '`tbl_issue`' (length=11)
          public 'primaryKey' => string 'id' (length=2)
          public 'sequenceName' => string '' (length=0)
          public 'foreignKeys' => 
            array (size=3)
              ...
          public 'columns' => 
            array (size=12)
              ...
          private '_e' (CComponent) => null
          private '_m' (CComponent) => null
      public 'columns' => 
        array (size=12)
          'id' => 
            object(CMysqlColumnSchema)[29]
              ...
          'name' => 
            object(CMysqlColumnSchema)[30]
              ...
          'description' => 
            object(CMysqlColumnSchema)[31]
              ...
          'project_id' => 
            object(CMysqlColumnSchema)[32]
              ...
          'type_id' => 
            object(CMysqlColumnSchema)[33]
              ...
          'status_id' => 
            object(CMysqlColumnSchema)[34]
              ...
          'owner_id' => 
            object(CMysqlColumnSchema)[35]
              ...
          'requester_id' => 
            object(CMysqlColumnSchema)[36]
              ...
          'create_time' => 
            object(CMysqlColumnSchema)[37]
              ...
          'create_user_id' => 
            object(CMysqlColumnSchema)[38]
              ...
          'update_time' => 
            object(CMysqlColumnSchema)[39]
              ...
          'update_user_id' => 
            object(CMysqlColumnSchema)[40]
              ...
      public 'relations' => 
        array (size=3)
          'owner' => 
            object(CBelongsToRelation)[41]
              ...
          'project' => 
            object(CBelongsToRelation)[42]
              ...
          'requester' => 
            object(CBelongsToRelation)[43]
              ...
      public 'attributeDefaults' => 
        array (size=0)
          empty
      private '_model' => 
        object(Issue)[23]
          private '_md' (CActiveRecord) => 
            &object(CActiveRecordMetaData)[24]
          private '_new' (CActiveRecord) => boolean false
          private '_attributes' (CActiveRecord) => 
            array (size=0)
              ...
          private '_related' (CActiveRecord) => 
            array (size=0)
              ...
          private '_c' (CActiveRecord) => null
          private '_pk' (CActiveRecord) => null
          private '_alias' (CActiveRecord) => string 't' (length=1)
          private '_errors' (CModel) => 
            array (size=0)
              ...
          private '_validators' (CModel) => null
          private '_scenario' (CModel) => string '' (length=0)
          private '_e' (CComponent) => null
          private '_m' (CComponent) => null
  private '_new' (CActiveRecord) => boolean false
  private '_attributes' (CActiveRecord) => 
    array (size=12)
      'id' => string '15' (length=2)
      'name' => string 'issue belong to project1' (length=24)
      'description' => string '123' (length=3)
      'project_id' => string '1' (length=1)
      'type_id' => string '2' (length=1)
      'status_id' => null
      'owner_id' => string '1' (length=1)
      'requester_id' => string '1' (length=1)
      'create_time' => null
      'create_user_id' => null
      'update_time' => null
      'update_user_id' => null
  private '_related' (CActiveRecord) => 
    array (size=0)
      empty
  private '_c' (CActiveRecord) => null
  private '_pk' (CActiveRecord) => string '15' (length=2)
  private '_alias' (CActiveRecord) => string 't' (length=1)
  private '_errors' (CModel) => 
    array (size=0)
      empty
  private '_validators' (CModel) => 
    object(CList)[47]
      private '_d' => 
        array (size=6)
          0 => 
            object(CRequiredValidator)[49]
              ...
          1 => 
            object(CNumberValidator)[50]
              ...
          2 => 
            object(CStringValidator)[51]
              ...
          3 => 
            object(CStringValidator)[52]
              ...
          4 => 
            object(CSafeValidator)[53]
              ...
          5 => 
            object(CSafeValidator)[54]
              ...
      private '_c' => int 6
      private '_r' => boolean false
      private '_e' (CComponent) => null
      private '_m' (CComponent) => null
  private '_scenario' (CModel) => string 'update' (length=6)
  private '_e' (CComponent) => null
  private '_m' (CComponent) => null
n8ghc7c1

n8ghc7c11#

I fixed it by below codes.

    public function actionUpdate($id)
    {

        $model=$this->loadModel($id);

             $this->loadProject($model->project_id);
             $model->project_id = $this->_project->id;

        if(isset($_POST['Issue']))
        {

            $model->attributes=$_POST['Issue'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('update',array(
            'model'=>$model,
        ));
    }

相关问题