0
votes

I'm using Symfony 1.4 with Doctrine in a project where users can upload an image as avatar. I don't want to store the filename in db but a boolean value. That's my relevant schema.yml section:

nyUserProfile:
...
has_avatar:
  type:    boolean
  notnull: true
  default: false

I overwritten the sfValidatedFile save method just to save some thumbnails with no call to parent::save function because I dont care about the original file:

    // Creamos los thumbnails
    $config = sfConfig::get('app_images_avatars');
    $original = new sfImage($this->getTempName());

    foreach($config['sizes'] as $thumbnail_name => $thumbnail_info){
        $thumb = $original->copy();
        $thumb->setQuality($thumbnail_info['quality']);
        $thumb->thumbnail($thumbnail_info['width'],$thumbnail_info['height'],$thumbnail_info['method'],'#000000');
        $thumb->saveAs($this->getPath() . DIRECTORY_SEPARATOR . $file . '_' .  $thumbnail_name . $thumbnail_info['extension'],$thumbnail_info['mime']);
    }

That works fine saving thumbs but boolean false value is stored into db at field has_avatar, I guess that's because parent::save method is not called at all but you know, I dont want to store the original file, just make thumbnails from it.

Is there a method to manually set the value stored in db just in case the image succesfully passed the validator tests?

2

2 Answers

0
votes

If your form is a Doctrine form, you can set the has_avatar property using the updateObject function of the form itself:

public function updateObject ($values = null)
{
    $object = parent::updateObject($values);
    $object->setHasAvatar(true);
            return $object;
}
-1
votes

You can do it in your action when the form is validated, just update the field in the db.

// Deal with the request
if ($request->isMethod('post'))
{
  $this->form->bind(/* user submitted data */);
  if ($this->form->isValid())
  {
    $object = $this->form->save();

    // your form is valid so your image is valid too
    $object->setHasAvatar(true);
    $object->save();
  }
}