繁体   English   中英

如何将新的管理页面添加到YII?

[英]How can I add new admin pages to YII?

我已经完成了YII,并且尝试复制管理页面以制作另一个页面。 admin.php包括:

<?php
/* @var $this UsersController */
/* @var $model banned */
//$model = 'Users';
$this->breadcrumbs=array(
    'Banned users'=>array('index'),
    'Manage',
);

/*
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
    $('.search-form').toggle();
    return false;
});
$('.search-form form').submit(function(){
    $('#banned-grid').yiiGridView('update', {
        data: $(this).serialize()
    });
    return false;
});
");*/
?>

<h1>Manage Banned Users</h1>

<p>
You may optionally enter a comparison operator (<b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</b>
or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.
</p>

<?php //echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">

<?php $this->renderPartial('_search1',array(
    'model'=>$model,
)); ?>
</div><!-- search-form -->

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'banned-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'uid',
        //'name',
        //'email',
        //'password',
        'datejoined',
        //'picture',
        /*
        'interestingquestionnotify',
        'myquestionnotify',
        'myanswernotify',*/
        'role',
        'status',
        //'newsletternotify',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

  <?php
 // print_r($model);
  ?>

我的问题是:当我打开该菜单点时,向我显示此管理php的内容,然后我可以看到另一个菜单点的zii.widgets.grid.CGridView。 need to be used the Banned.php来自models文件夹need to be used the Banned.phpbut this one use Users.php目录but this one use Users.php 知道如何调试吗?

我在模型中有Banned.php。 而且我已禁止上课。 我禁止查看。

这是我的protected / controllers / BannedController.php

<?php

class BannedController 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'.
     */
    public $layout='//layouts/column1';

    /**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
            'postOnly + delete', // we only allow deletion via POST request
        );
    }

    /**
     * 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 admin user to perform 'admin' and 'delete' actions
                'actions'=>array('index','view','admin','delete','create','update'),
                '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)
    {
        $model = $this->loadModel($id);
        $questions = new Questions('search');
        $ans = new Answers('search');

        // válaszok
        if(isset($_GET['Answers'])){
            $ans->attributes = $_GET['Answers'];
            $ans->uid = $model->uid;
            $answersDataProvider = $ans->searchOnUser();
        } else {
            $answersDataProvider = new CActiveDataProvider('Answers', array(
                'criteria'=>array('condition'=>'uid='.$model->uid),
                'pagination'=>array('pageSize'=>10),
            ));
        }

        //kérdések
        if(isset($_GET['Questions'])){
            $questions->attributes = $_GET['Questions'];
            $questions->uid = $model->uid;
            $questionsDataProvider = $questions->searchOnUser();
        } else {
            $questionsDataProvider = new CActiveDataProvider('Questions', array(
                'criteria'=>array('condition'=>'uid='.$model->uid),
                'pagination'=>array('pageSize'=>10),
            ));
        }

        $this->render('view',array(
            'model'=>$model,
            'answersDataProvider'=>$answersDataProvider,
            'questionsDataProvider'=>$questionsDataProvider,
            'ans'=>$ans,
            'questions'=>$questions
        ));
    } */
    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 Banned;

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

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

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

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);

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

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

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

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete($id)
    {
        DMongo::get()->selectCollection('users')->remove(array('_id' => intval($id)));
        $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'));
    }

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

    /**
     * Manages all models.
     */
    public function actionAdmin()
    {
        $model=new Users('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Users']))
            $model->attributes=$_GET['Banned'];
                                                    // Users
        $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 $id the ID of the model to be loaded
     * @return Users the loaded model
     * @throws CHttpException
     */ 
    public function loadModel($id)
    {
        $model=Banned::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist1.');
        return $model;
    }

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

这是:protected / models / Banned.php

<?php

/**
 * This is the model class for table "tbl_users".
 *
 * The followings are the available columns in table 'tbl_users':
 * @property integer $uid
 * @property string $name
 * @property string $email
 * @property string $password
 * @property string $datejoined
 * @property string $picture
 * @property integer $interestingquestionnotify
 * @property integer $myquestionnotify
 * @property integer $myanswernotify
 * @property string $role
 * @property integer $status
 * @property integer $newsletternotify
 */
class Banned extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Banned the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'tbl_banned_users';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('user_id, user_mail, user_name'),
            array('interestingquestionnotify, myquestionnotify, myanswernotify, status, newsletternotify', 'numerical', 'integerOnly'=>true),
            array('name, email, password, picture', 'length', 'max'=>50),
            array('role', 'length', 'max'=>10),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('uid, name, email, password, datejoined, picture, interestingquestionnotify, myquestionnotify, myanswernotify, role, status, newsletternotify', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'uid' => 'Azonosító1',
            'name' => 'Név',
            'email' => 'E-mail',
            'password' => 'Jelszó',
            'datejoined' => 'Dátum',
            'picture' => 'Profilkép',
            'interestingquestionnotify' => 'Megfigyelt kérdésekről értesítés',
            'myquestionnotify' => 'Kérdésekről értesítés',
            'myanswernotify' => 'Válaszokról értesítés',
            'role' => 'Jog',
            'status' => 'Állapot',
            'newsletternotify' => 'Hírekről értesítés',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('uid',$this->uid);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('email',$this->email,true);
        $criteria->compare('password',$this->password,true);
        $criteria->compare('datejoined',$this->datejoined,true);
        $criteria->compare('picture',$this->picture,true);
        $criteria->compare('interestingquestionnotify',$this->interestingquestionnotify);
        $criteria->compare('myquestionnotify',$this->myquestionnotify);
        $criteria->compare('myanswernotify',$this->myanswernotify);
        $criteria->compare('role',$this->role,true);
        $criteria->compare('status',$this->status);
        $criteria->compare('newsletternotify',$this->newsletternotify);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

    public function beforeSave() {
        if ($this->isNewRecord)
            $this->password = md5($this->password);

        return parent::beforeSave();
    }
}

而且我的BannedController不能从Banned.php获取数据,它可以从Users.php获取数据。现在我将所有用户替换为Banned,但是它写道: Banned has an invalid validation rule. The rule must specify attributes to be validated and the validator name. Banned has an invalid validation rule. The rule must specify attributes to be validated and the validator name.

好,让我们检查一下

在您的actionCreate方法中,如图所示

public function actionCreate()
    {
        $model=new Banned;

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

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

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

并在您的actionAdmin方法中执行如下所示

 public function actionAdmin()
    {
        $model=new Banned('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Banned']))
            $model->attributes=$_GET['Banned'];
                                                    // Users
        $this->render('admin',array(
            'model'=>$model,
        ));
    }

只是尝试看看

检查显示此视图的控制器方法,例如,U可能已将用户作为模型发送给该视图

$model=new User();
$this->render('UrViewFileName',array('model'=>$model);

相反,您发送您的被禁模型

$banned=new Banned();
$this->render('UrViewFileName',array('model'=>$banned);

我不确定,但我认为这可能是您的问题

正如@Ninad在$model上方所说,您正在渲染的多个视图的类型为Users而不是根据需要Banned 将所有Users实例替换为“ Banned以更正此问题。 管理员方法应为

public function actionAdmin()
{
    $model=new Banned('search'); 
    $model->unsetAttributes();  // clear any default values
    if(isset($_GET['Banned']))
        $model->attributes=$_GET['Banned'];
                                                // Users
    $this->render('admin',array(
        'model'=>$model,
    ));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM