Putting Data Directly Into A Model

Hello guys,

I have a model LatestCrop, and I was wondering how I could append data into it? It has only one field, lat_crop.

I wanted to append a certain data using php. How can I do this?

Do you mean just create a new record into table LatestCrop?




$my_variable='Some data';

$LatestCrop=new LatestCrop;

$LatestCrop->lat_crop=$my_variable;

$LatestCrop->save();



Is that what you are referring to? If not could you explain further.

Is there anyway I could update it. There is only one record I must have on lat_crop. I don’t need $LatestCrop=new LatestCrop;

If you need to update an existing field and you know it’s PK




$latestcrop = LatestCrop::model()->findByPk($pk);


$latestcrop->lat_crop = $my_variable;


$LatestCrop->save();







If you use the beforeSave() inside your model function then you can use $this keyword




$this->lat_crop = $my_variable;







Hope that helps

If there is only one field there is no id only lat_crop




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

    ->select('*')

    ->from('latest_crop')

    ->queryRow();


$cropdata->lat_crop = $my_variable;

$cropdata->save();