Yii Framework Forum: Presentation Of Data - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Presentation Of Data getters vs helpers Rate Topic: -----

#1 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,700
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 17 February 2014 - 04:13 PM

Hi guys.

Suppose we have a model which data is not human-readable (timestamps instead of dates and so on).
So we need to convert it to readable form.

How do you prefer to implement it?
God is real unless declared as integer
0

#2 User is offline   nineinchnick 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 622
  • Joined: 12-September 11
  • Location:Bialystok, Poland

Posted 18 February 2014 - 01:10 AM

Use a formatter in the views. I use this for data that is stored in a different format for computational reasons (monetary values) but needs to be formatted for display (3000 vs 30,00).

This is trivial in grids/list views and other widgets and adds a bit of code in active forms, where you need to override the attribute's value by specifying the 'value' option.

I was considering formatting the whole model before passing it to the view but there are cases where you display same value in the views with slightly different formatting. And also this is the presentation layer and that's what for formatters are.
Don't be a dick.
0

#3 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,700
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 18 February 2014 - 02:18 AM

View Postnineinchnick, on 18 February 2014 - 01:10 AM, said:

And also this is the presentation layer and that's what for formatters are.


Yeah, I know :(

Actually, the whole thing started when I was working with dates/datetimes heavily. So I had a lot of datepickers in the views (human readable format) and they must be converted to DB format (timestamp w/timezone) on saving. It seemed logical to me to create virtual attributes (created, updated) and setters that converts value to timestamp and sets actual field value (created_at, updated_at).
So the form field was created upon virtual attribute, and the getter was required also. It was way more convenient to have getters/setters instead of afterFind/beforeSave.

Then I started to overuse these things probably. Import CSV? no problem, just create a dozen of setters that convert the values.
User submits site URL? okay, let's use a setter to remove 'http(s)://' part (also I had used custom validators for this, wait, WAT?!). And when we have setter, there's always getter...

Bad thing is I lose all this stuff when it comes to DAO queries. No AR, no getters. Seems like one layer is missing (between DB and AR probably).
And yes, it's presentational logic :(

Ok, so how would you implement this: user can submit form that has human-readable data (date, time, phone) that must be stored in another format?

This

Quote

adds a bit of code in active forms, where you need to override the attribute's value by specifying the 'value' option.

is only for displaying, right? the storage logic must be somewhere else.
God is real unless declared as integer
0

#4 User is offline   nineinchnick 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 622
  • Joined: 12-September 11
  • Location:Bialystok, Poland

Posted 18 February 2014 - 03:00 PM

View PostORey, on 18 February 2014 - 02:18 AM, said:

Bad thing is I lose all this stuff when it comes to DAO queries. No AR, no getters. Seems like one layer is missing (between DB and AR probably).
And yes, it's presentational logic :(


Even when using a formatter you'd have to store the raw values somewhere and pass them to a formatter, either manually or through a widget (like grid). So why not just populate a model with values obtained using DAO.

View PostORey, on 18 February 2014 - 02:18 AM, said:

Ok, so how would you implement this: user can submit form that has human-readable data (date, time, phone) that must be stored in another format?


I'd have a model for the form and do as I said before, filter data using filters in validation rules or standalone and format it in the view when drawing the form.

View PostORey, on 18 February 2014 - 02:18 AM, said:

This

is only for displaying, right? the storage logic must be somewhere else.


As I said, I filter data using filters in validation rules. I do think about extracting it from there and calling it once just after assigning POST data, like the load() method from Yii2.
Don't be a dick.
0

#5 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,700
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 18 February 2014 - 03:10 PM

ok thx
God is real unless declared as integer
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users