Hi I’m new to yii i’m having a problem with my simple application having a create and view function. My data did not populate in my CGridView. My code looks like this:
here’s my Student.php (model)
class Student extends CFormModel{
public $id;
public $fname;
public $lname;
public $midinitial;
public $yrlevel;
public $isNewRecord;
public function tableName()
{
return 'student';
}
public function attributeLabels()
{
return array(
'id' => 'Student ID',
'fname' => 'First Name',
'lname' => 'Last Name',
'midinitial' => 'Middle Initial',
'yrlevel' => 'Year level',
);
}
public function viewStudent(){
$conn = Yii::app()->db;
$sql = "Select * from student";
$command = $conn->createCommand($sql);
$result = $command->queryAll();
return $result;
}
public function createStudent(){
$conn = Yii::app()->db;
$sql = "INSERT INTO student(fname,lname,midinitial,yrlevel)VALUES(:fname,:lname,:midinitial,:yrlevel)";
$command = $conn->createCommand($sql);
$command->bindParam(":fname",$fname,PDO::PARAM_STR);
$command->bindParam(":lname",$lname,PDO::PARAM_STR);
$command->bindParam(":midinitial",$midinitial,PDO::PARAM_STR);
$command->bindParam(":yrlevel",$yrlevel,PDO::PARAM_STR);
$command->execute();
}
and here’s my StudentController.php (controller)
class StudentController extends Controller {
public $layout='//layouts/column1';
public function actionIndex()
{
$model = new Student();
$result = $model->viewStudent();
$dataProvider = new CArrayDataProvider($result);
$this->render('index',array('dataProvider'=>$dataProvider));
}
public function actionView(){
$model = new Student();
$result = $model->viewStudent();
$dataProvider = new CArrayDataProvider($result);
$this->render('index',array('dataProvider'=>$dataProvider));
}
/* public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}*/
public function actionCreate(){
$model = new Student();
if(isset($_POST['Student']))
{
$model->attributes=$_POST['Student'];
//if($model->createStudent())
$this->redirect(array('index','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
'create'=>$model,// Model is passed to create.php View!
));
}
and here’s my index
<?php
$this->breadcrumbs=array(
'student',
);
$this->menu=array(
array('label'=>'View Student', 'url'=>array('view')),
array('label'=>'Create Student', 'url'=>array('create')),
);
?>
<h1>Student</h1>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'student-grid',
'dataProvider'=>$dataProvider,
'columns'=>array(
'id',
'fname',
'lname',
'midinitial',
'yrlevel',
),
)); ?>
please guide me in proper way. Advance thank you to someone can help me.