Hi… I’m pretty new to Yii and I have been creating an admin system. I got stuck with my cgridview, I want to have one textfield from a fuction in my model and a dropdownlist. I have looked around and can’t really find the answer I’m looking for online. Think it’s my controller that i am doing wrong. I want to save those fields to different table in my database.
DB tables used
Visitor_master (name,surname,mobile,email, invited by)
visitor_comment (id,comment,visitor_id,date)
visitor_status (id, description, visitor_id, date)
In my view… admin
/************************************************************************************************************/
<?php $form=$this->beginWidget(‘CActiveForm’, array(
'id'=>'menu-grid',
'enableAjaxValidation'=>true,
)); ?>
<?php $this->widget(‘zii.widgets.grid.CGridView’, array(
'id'=>'menu-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'name',
'surname',
'mobile',
'email',
'invited_by',
array(
'header'=>'comment',
'value'=>'CHTML::textField($test->comment,$data->Comments(),array(\'width\'=>20,\'maxlength\'=>3))',
'type'=>'raw',
'htmlOptions'=>array('width'=>'20px'),
),
array(
'header' => 'Comment',
'type' => 'text',
'value' => ('$data->Comments()'),
),
array(
'header' => 'status',
'type' => 'text',
'value' => ('$data->Statuses()'),
),
array(
'class'=>'CButtonColumn',
'template'=>'{update}',
),
),
)); ?>
<div class="row buttons">
<?php echo CHtml::submitButton($test->isNewRecord ? 'Save' : 'Save'); ?>
<!--<button type="sumbit">Next Step</button>-->
</div>
<?php $this->endWidget(); ?>
/***********************************************************************************************************/
model : visitor_master
/************************************************************************************************************/
public function Comments()
{
$comments = array();
$count_array = count($this->visitorComments);
$v=0;
foreach($this->visitorComments as $comment){
$v++;
$comments[] = $comment->comment;
if ($count_array = $v){
//echo implode($comments);
}
return implode($comments);
}
}
// function to diplay all status for the visitors
public function Statuses()
{
$statuses = array();
$count_array = count($this->visitorStatuses);
$v=1;
foreach($this->visitorStatuses as $status){
if ($count_array = $v){
$statuses[] = $status->description;
}
return implode("\n", $statuses);
$v++;
}
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'visitor_master';
}
/**
* @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('name, surname, mobile, email, invited_by, leader_id', 'required'),
array('name, surname, invited_by', 'length', 'max'=>30),
array('mobile', 'length', 'max'=>12),
array('email', 'length', 'max'=>40),
array('leader_id', 'length', 'max'=>20),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, name, surname, mobile, email, invited_by, leader_id', '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(
'visitorComments' => array( self::HAS_MANY, 'visitor_comment', 'visitor_id'),
//'order'=>'VisitorComments.comment DESC'),
'visitorEntries' => array(self::HAS_MANY, 'visitor_entry', 'visitor_id'),
'leader' => array(self::BELONGS_TO, 'leader_master', 'leader_id'),
'visitorStatuses' => array(self::HAS_MANY, 'visitor_status', 'visitor_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'name' => 'Name',
'surname' => 'Surname',
'mobile' => 'Mobile',
'email' => 'Email',
'invited_by' => 'Invited By',
'leader_id' => 'Leader',
);
}
/**
* 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.
$action = $_GET['id'];
if (!isset($_GET['id'])){
$action = Yii::app()->session['min_leader_id'];
}
//if ($action = 0){
//$action = Yii::app()->session['leader_id'];
//}
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('name',$this->name,true);
$criteria->compare('surname',$this->surname,true);
$criteria->compare('mobile',$this->mobile,true);
$criteria->compare('email',$this->email,true);
$criteria->compare('invited_by',$this->invited_by,true);
$criteria->compare('leader_id',$this->leader_id,true);
$criteria->addCondition("leader_id = $action");
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
/*************************************************************************************************************/
Controller
public function actionAdmin()
{
$model=new visitor_master('search');
$test = new visitor_comment;
//if(Yii::app()->request->isPostRequest)
//{
$fakeid = 82;
$date=date('y-m-d');
if(isset($_GET['visitor_master'],$_GET['visitor_comment'] )){
$test->date=$date;
$test->visitor_id=$fakeid;
$test->comment='Hello';
$model->attributes=$_GET['visitor_master'];
$test->attributes=$_GET['visitor_comment'];
// $model->save();
$test->save();
//}
}
// $model->unsetAttributes(); // clear any default values
$this->render('admin',array(
'model'=>$model,
'test'=>$test,
));
}
Please if someone can help me, have been stuck on this for age now