fandi17
(Sastra Afandi)
August 5, 2013, 6:55am
1
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
jayant
(Codesutras)
August 5, 2013, 7:02am
2
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();
jayant
(Codesutras)
August 5, 2013, 7:04am
3
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.
shgninc
(Shgninc)
August 5, 2013, 7:13am
4
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());
fandi17
(Sastra Afandi)
August 5, 2013, 7:35am
5
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();
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…
jayant
(Codesutras)
August 5, 2013, 8:49am
7
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.
fandi17
(Sastra Afandi)
August 7, 2013, 4:04am
8
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