How To Return Database Fetch Value

Hi,

I want to list details of a particular Student. For this write the following in view.php page of student.


$this->widget('zii.widgets.CDetailView', array(

	'data'=>$model,

	'attributes'=>array(

		'Student_ID',

		'Student_Name',

		'Student_Code',

		array(

            'label'=>'Group',

			'value' => $model->set_group_id($model->Group_ID)

        ),



Declare set_group_id() function as following in student model page


public static function set_group_id($id)

    {

		$val= Yii::app()->db->createCommand()->select('Group_Name')->from('m_student_group')->where('Group_Id=:$id')->queryAll(array('index'=>'Group_Name'));

		var_dump($val);

		return $val->Group_Name;

}

Now it shows following error.


CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':$id' at line 3. The SQL statement executed was: SELECT `Group_Name`

FROM `m_student_group`

WHERE Group_Id=:$id 

In the above sql query instead of $id put hard code value (say 52) then it shows error


Trying to get property of non-object 

Please help me how to solve this problem.

Set the relation between two table

for example

category


id

category

status

category


id

category_id

subcategory

status

in models/subCategory.php




public function relations() {

        return array(

            'category' => array(self::BELONGS_TO,'Category', 'id'),        );

    }



view/subcategory/admin.php




$this->widget('zii.widgets.grid.CGridView', array(

        'id'=>'sub-category-grid',

        'dataProvider'=>$model->search(),

        'filter'=>$model,

        'columns'=>array (

                'id',

                 array(

                    'name'=>'category_search',

                    'header'=>'Category',

                    'sortable'=>false,

                    'value'=>$data->category->category

                  ),

                'sub_category',

                'status',

                array(

                        'class'=>'CButtonColumn',

                ),

        ),

));



Hi Balu, for your kind information i am already written the following code in student.php model page of student page.


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(

			'Ledger' => array(self::BELONGS_TO, 'StudentGroup', 'Group_Id'),

		);

	}

But and wrote the following code at studentGroup.php model page of studentgroup.


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(

			'Group' => array(self::HAS_MANY, 'Student', 'Group_Id'),

		);

	}

Please tell me how to solve this problem.

add this





$this->widget('zii.widgets.CDetailView', array(

        'data'=>$model,

        'attributes'=>array(

                'Student_ID',

                'Student_Name',

                'Student_Code',

                  array(

                        name'=>'Group',

                        'header'=>'Group',

                        'sortable'=>true,

                         'value'=>$data->Group->Group_Name

                        ),

        ),









Please try like this