2013-05-28 57 views
0

我有一個完整的YII,我試圖複製一個管理頁面來製作另一個。 的admin.php的inclue這樣的:如何將新的管理頁面添加到YII?

<?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。我從型號文件夾but this one use Users.php中選擇need to be used the Banned.php。任何想法我怎麼可以調試它?

我Banned.php的車型。我有禁止班。我已經禁止了意見。

這是我的保護/控制器/ 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(); 
     } 
    } 
} 

這就是:保護/模型/ 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得到DATAS,它從Users.php獲取 現在我將所有用戶都替換爲禁止,但它寫道:Banned has an invalid validation rule. The rule must specify attributes to be validated and the validator name.

+0

顯示此視圖的控制器方法是什麼? – topher

+0

我更新了我的問題。 – user2301881

回答

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

試試吧,看看

1

檢查控制器方法哪個d isplays這一觀點例如如圖

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

U可以已發送的用戶爲模型視圖 而是你寄你的禁止模型

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

我不知道,但我認爲這可能是烏爾問題

+0

我更新了我的問題。 – user2301881

+0

請給我你上面發佈的視圖文件的名稱 – Ninad

+0

你的禁止控制器的actionAdmin方法是否呈現這個視圖? – Ninad

1

正如@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, 
    )); 
} 
相關問題