2013-05-29 81 views
0

即時通訊此新YII框架,是我的工作所需要的,我一直有這個錯誤,雖然我有意見/用戶/管理,UserController和用戶(模型)我不知道它有什麼問題,我完全迷茫..我跳有人可以幫助我在這裏..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

這裏:

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

$用戶未在此方法的範圍內定義。

如果它是一個數據庫對象,你必須檢索它作爲一個ActiveRecord:

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

或等價的東西。

如果是應用程序用戶:Yii::app()->user有竅門。

更有可能的是,你已經擁有了它作爲$模型

$model = new User('search'); 

這取決於你這個$用戶變量,你期待的觀點做什麼。 強調文本

+0

感謝您的幫助,我得到了它爲什麼有什麼問題..我在控制器錯誤消息中uncluded'load_model',它顯示代碼有什麼問題..我只需在'密碼「,並修復了一切......但我不明白爲什麼錯誤有點不同 – Megan