Is it possible to populate model variables with JSON data

Suppose I have this in my controller:


public function actionEditEvent() /* ajax function */

{

	$event = Event::model()->findByPk($_POST['id']);

	

	$return_array = array(

		'data'=>array(

			'date'=>$event->date,

			'time'=>$event->time,

			'comments'=>$event->comments,

			'id'=>$event->id,

		),

	);

	

	echo json_encode($return_array);

}

And in my view I have this:


$('#list').on('click', '.edit-button', function(){

	var dataId = $(this).attr('data-id');

	

	$.ajax({

		url: 'booking/editevent',

		type: 'POST',

		dataType: 'JSON',

		data: {id: dataId},

		success: function(result){

			$('#add-event #Event_date').val(result.data.date);

			$('#add-event #Event_time').val(result.data.time);

			$('#add-event #Event_comments').val(result.data.comments);

			$('#add-event #Event_id').val(result.data.id);

		}

	});

});

As you can see in my ajax ‘success’ handler I am having to manually assign the values from the JSON object. Is it possible for this to be done in one single assignment?

#add-event is a CActiveForm

Maybe :




$.each(result.data, function(name, value){

   if($('#add-event #Event_'+name).length > 0) 

      $('#add-event #Event_'+name).val(value);

});



No I mean is there any built-in Yii way of doing it?

no it isn’t.

Yii is PHP, you need a JS solution.

I know I need a JS solution. What I’m asking is whether there is any Yii helper function to do this.