views:

63

answers:

1

Hello, guys!

Assume a Doctrine model Profile:

# This is example of my schema.yml
Profile:
 columns:
   avatar:
     type: string(255)
     notnull: true

My goal is to generate profile's avatar from uploaded file:

class Avatar extends BaseAvatar{
  public function postSave($e){
    if($this->getAvatar()){
      // resize/crop it to 100x100
      // and save
    }
}

That logic is fine for me now. But my Profile associated record is updated on every request with some extra info. And, as you can see, avatar file is generated over and over in spite of the fact that avatar field may stay the same.

Question: How can framework determine whether the particular field is updated or not?

Note: I don't need updating in symfony's actions because of code repeating in several apps. Or maybe I need?

A: 

If you are using a Form to render the profile editing fields I would suggest moving your resizing code into there by overriding the saveFile method you inherit from sfFormDoctrine:

protected function saveFile($field, $filename = null, sfValidatedFile $file = null)
{
  $finalFilename = parent::saveFile($field, $filename, $file);
  if($field == 'avatar')
  {
    // generate thumbnail from $finalFilename
  }
}

That will get called when the save() method is called on your form. Hope that helps.

Cryo