YII未定义变量

时间:2013-05-29 07:15:47

标签: php yii

我是我的工作所需的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',
    //),
//),
//)); 

UserController

  <?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',
    //),
//),
//)); 







?>

1 个答案:

答案 0 :(得分: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 变量。强调文字