Model Customization? Help!

I'm working on a Web Application that involves saving latitude/longitude for certain points to a database and I'm using PostgreSQL's geometry type to do this.

To grab the lat/lng from the database you have to do something like: 

select x(geometry) as latitude, y(geometry) as longitude

and for saving there is a database function that takes lat/lng as parameters.

I was wondering if there's a way to extend my model in Yii to automatically grab the latitude/longitude from the database when a find is called.  And then to automatically save the latitude/longitude using the database function when I save the model.

Thanks for your help!

To make it work with find calls:

  1. Declare two public members: latitude and longitude.

  2. Set query criteria's "select" option so that it contains 'x(geometry) as latitude, y(geometry) as longitude'.

To save back to database:

  1. Override beforeSave():

Hey thanks very much for your help.

This method seems to work except for one thing, after a select I can get the lat/lng using $model->latitude but if I do $model->getAttributes() it isn't included.  Not really a huge deal but is there a way to get an array of all the selected attributes with lat/lng included?