0

我有一个完整的 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 来自模型文件夹,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.

4

3 回答 3

2

好的,让我们检查一下

在您的 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,
        ));
    }

试试看

于 2013-05-29T12:23:04.087 回答
1

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

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

相反,你发送你的禁止模型

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

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

于 2013-05-29T05:35:05.280 回答
1

正如@Ninad 上面所说,$model您正在使用类型Users而不是Banned所需的方式渲染几个视图。替换 的所有实例UsersBanned更正此问题。管理方法应该是

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,
    ));
}
于 2013-05-29T07:59:02.543 回答