0

我是我工作所需的 YII 框架的新手,尽管我有视图/用户/管理员、用户控制器和用户(模型),但我一直有这个错误,我不知道它有什么问题,我完全糊涂了。有人可以在这里帮助我..

 PHP notice
Undefined variable: users

C:\xampp\htdocs\winegmobile\protected\controllers\UserController.php(147)

135      */
136     public function actionAdmin()
  137     {
138         $model=new User('search');
 139         $model->unsetAttributes();  // clear any default values
 140         if(isset($_GET['User']))
141             $model->attributes=$_GET['User'];
 142 
143         $this->render('admin',array(
 144             'model'=>$model,
145                     
146                         'User'=>$User
147         ));
148     }
149 
150     /**
151      * Returns the data model based on the primary key given in the GET variable.
152      * If the data model is not found, an HTTP exception will be raised.
153      * @param integer $id the ID of the model to be loaded
154      * @return User the loaded model
155      * @throws CHttpException
156      */
157     public function loadModel($id)
158     {
159         $model=User::model()->findByPk($id);


      This is the view/User/admin
   <?php
   /* @var $this UserController */
   /* @var $model User */

    $this->breadcrumbs=array(
'Users'=>array('index'),
'Manage',
   );

   $this->menu=array(
array('label'=>'List User', 'url'=>array('index')),
array('label'=>'Create User', 'url'=>array('create')),
   );

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

    <h1>Manage Users</h1>
     <?php $this->widget('bootstrap.widgets.TbBreadcrumbs', array(
   'links'=>array('Admin'=>'index.php?r=site/admin', 'User'))); ?>
    <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('_search',array(
'model'=>$model,

   'User'=>$User,

   )); ?>
  </div><!-- search-form -->

  <?php 
  //change starts here

  $this->widget('bootstrap.widgets.TbGridView', array(
'type'=>'striped bordered condensed',
'dataProvider'=>$model->search(),
 'filter'=>$model,
'template'=>"{items}\n{pager}",
'columns'=>array(
    array('name'=>'username', 'header'=>'Username'),
     array('name'=>'FirstName', 'header'=>'First Name'),
     array('name'=>'LastName', 'header'=>'Last Name'),
     array('name'=>'Email', 'header'=>'Email'),
     array('name'=>'password', 'header'=>'Password'),
    array(
        'class'=>'bootstrap.widgets.TbButtonColumn',
        'htmlOptions'=>array('style'=>'width: 50px'),
    ),      
   ),
    )); 






    //end







  //$this->widget('zii.widgets.grid.CGridView', array(
//'id'=>'user-grid',
//'dataProvider'=>$model->search(),
//'filter'=>$model,
//'columns'=>array(
    //'id',
//  'FirstName',
//  'LastName',
//  'Email',
//  'Password',
//  'address_id',
//  array(
    //  'class'=>'CButtonColumn',
    //),
//),
//)); 

用户控制器

  <?php
  /* @var $this UserController */
  /* @var $model User */

 $this->breadcrumbs=array(
 'Users'=>array('index'),
'Manage',
 );

  $this->menu=array(
array('label'=>'List User', 'url'=>array('index')),
array('label'=>'Create User', 'url'=>array('create')),
  );

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

      <h1>Manage Users</h1>
     <?php $this->widget('bootstrap.widgets.TbBreadcrumbs', array(
     'links'=>array('Admin'=>'index.php?r=site/admin', 'User'))); ?>
     <p>




      <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('_search',array(
'model'=>$model,


     )); ?>
     </div><!-- search-form -->

     <?php 
     //change starts here

      $this->widget('bootstrap.widgets.TbGridView', array(
      'type'=>'striped bordered condensed',
     'dataProvider'=>$model->search(),
     'filter'=>$model,
     'template'=>"{items}\n{pager}",
     'columns'=>array(
       array('name'=>'username', 'header'=>'Username'),
       array('name'=>'FirstName', 'header'=>'First Name'),
       array('name'=>'LastName', 'header'=>'Last Name'),
       array('name'=>'Email', 'header'=>'Email'),
       array('name'=>'password', 'header'=>'Password'),
      array(
        'class'=>'bootstrap.widgets.TbButtonColumn',
        'htmlOptions'=>array('style'=>'width: 50px'),
       ),      
     ),
    )); 






   //end







 //$this->widget('zii.widgets.grid.CGridView', array(
//'id'=>'user-grid',
//'dataProvider'=>$model->search(),
//'filter'=>$model,
//'columns'=>array(
    //'id',
//  'FirstName',
//  'LastName',
//  'Email',
//  'Password',
//  'address_id',
//  array(
    //  'class'=>'CButtonColumn',
    //),
//),
//)); 







?>
4

1 回答 1

1

这里 :

public function actionAdmin() {
    $model = new User('search');
    $model->unsetAttributes();  // clear any default values
    if (isset($_GET['User']))
    $model->attributes = $_GET['User'];

    $this->render('admin', array(
    'model' => $model,
    'User' => $User // <------ UNDEFINED
    ));
}

$User未定义在此方法的范围内。

如果它是数据库对象,则必须将其作为 ActiveRecord 检索:

MyTable::model()->findByPk($theUSerId)

或等效的东西。

如果是应用程序用户:Yii::app()->user就可以了。

更有可能的是,您已经将其作为$model

$model = new User('search');

这取决于您对转发到视图的$User变量所做的操作。强调文本

于 2013-05-29T07:40:47.267 回答