2013-02-22 2 views
1

저는 Yii를 처음 사용하고 있으며 지금 배우고 있습니다. 여기 나는 데이터베이스의 사용자 테이블에서 사용자의 목록을 얻으려고합니다. 다음 내 사용자입니다 모델정의되지 않음 변수 : model

class UsersController extends Controller 
{ 
    public function actionIndex() 
    { 
     $this->render('index'); 
    } 

    public function actionView() 
    { 
     $model = new Users; 

     $this->render('view',array(
     'model'=>$model, 
     )); 
    } 
} 

: 내보기에 따라

class Users extends CActiveRecord 
{ 

public static function model($className=__CLASS__) 
{ 
    return parent::model($className); 
} 

/** 
* @return string the associated database table name 
*/ 
public function tableName() 
{ 
    return '{{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('fname, lname, email', 'required'), 
     array('fname, lname', 'length', 'max'=>50), 
     array('email', 'length', 'max'=>100), 
     // The following rule is used by search(). 
     // Please remove those attributes that should not be searched. 
     array('id, fname, lname, email', '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(
     'id' => 'ID', 
     'fname' => 'First Name', 
     'lname' => 'Last Name', 
     'email' => 'Email', 
    ); 
} 

/** 
* 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('id',$this->id); 
    $criteria->compare('fname',$this->fname,true); 
    $criteria->compare('lname',$this->lname,true); 
    $criteria->compare('email',$this->email,true); 

    return new CActiveDataProvider($this, array(
     'criteria'=>$criteria, 
    )); 
} 
} 

된다

<h1>Users</h1> 

<p> 
Below is the list of users, here you may add user. 
</p> 

<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model, 
'attributes'=>array(
    'id', 
    'fname', 
    'lname', 
    'email', 
), 
)); ?> 

<div class="view"> 

<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b> 
<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?  > 
<br /> 

<b><?php echo CHtml::encode($data->getAttributeLabel('fname')); ?>:</b> 
<?php echo CHtml::encode($data->fname); ?> 
<br /> 

<b><?php echo CHtml::encode($data->getAttributeLabel('lname')); ?>:</b> 
<?php echo CHtml::encode($data->lname); ?> 
<br /> 

<b><?php echo CHtml::encode($data->getAttributeLabel('email')); ?>:</b> 
<?php echo CHtml::encode($data->email); ?> 
<br /> 

</div> 

내가 PHP를 얻고

다음은 뷰 내 사용자 컨트롤러 기능입니다 정의되지 않은 변수 : 모델이라는 것을주의하십시오. 미리 감사드립니다.

+0

모든 사용자에게 표시 하시겠습니까? 그런 다음'zii.widgets.CListView'와 같은 것을 사용해야합니다. Btw,보기 'actionIndex' 또는'actionView' 무엇입니까? –

+0

답장을 보내 주셔서 감사합니다.보기가 actionview입니다. –

답변

0

_view.php를 붙여 넣은 것처럼 보입니다. view.php에서 $model 변수를 _view.php으로 넘겨야합니다. 이는 actionView()에서 '보기'로만 전달되기 때문입니다.

+0

답장을 보내 주셔서 감사합니다.하지만 더 구체적으로 할 수있는 것과 같습니다. –

+0

$ model 변수를 view.php에서 _view.php로 가져와야합니다. view.php에서'renderPartial()'호출을보세요 :) –

+0

고마워요 !!! –