Implement Query Sql In Yii

I have read a function in model

public function getData(){

    $data=Yii::app()->db->createCommand()


        ->select('data_student')


        ->from('data_student_PHP')


        ->where('years=YEARS(CURDATE())')


        ->query();


    return $data;

How to transfer this value to controller and view????. I need help…pleasee

Simply call this method from your controller or view file by creating instance of your model class.Like




$dataResult = Modelname::model()->getData(); 


Or

$model = new ModelName();

$dataResult =$model->getData(); 




Also if you are writing this code in controller action then you need to pass this data into render method’s array to get this info in view file.




$this->render('viewfile',array('data'=>$data));


So, in view file you can access that data with $data variable.



Also in each view file you have a property $model which refer to your model class, so you can use it as below:




print_r($model->getData());



how to print the value???. I`ve try to use "echo" but "Object of class CDbDataReader could not be converted to string"

Hi,

you can use CDbCriteria


public function getData(){

                $criteria = new CDbCriteria;

		$criteria->select = 'data_student';

		$criteria->addCondition("years=YEARS(CURDATE())");

		$data    =  ModelName::model()->find($criteria);

		return $data;

}

and call this function on model




$model = new ModelName();

$data =$model->getData(); 

print_r($data)



Note : if you want to fetch all record you want to add findAll condition

i think it’s works…

could you please show me your code?

Since if you are returning data from your method which is a Model class object. So, if you want to access any particular attribute from that then you should access it like.




echo $data->year;

echo $data->name; 



i hope you got my point.

I`ve got it… it has been solved… thanks

great ;)