Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine postSave, postUpdate and Internationalization (detect modification)

I'm having a little problem with a tables that are using the i18n behaviour on a Symfony 1.4 project that I'm developing. For example on the following model defined on YAML (I have others that follow the same pattern):

Subject:
  actAs:
    Timestampable: ~
    I18n:
      fields: [name]
  columns:
    name: { type: string(255), notnull: true }
  relations:
    Publications:
      class: Publication
      refClass: PublicationSubject
      local: subject_id
      foreign: publication_id

I only have the name field that is internationalized but on save (after altering one of the languages on a form) the postUpdate($event) method doesn't get triggered. I thought, well I can use the postSave($event) method and check if it is modified but it also always returns false. So how do I detect if a Internationalized Doctrine model got modified?

Thanks in advance ;)

like image 487
petersaints Avatar asked Nov 15 '22 07:11

petersaints


1 Answers

The short answer I found is, there is no a easy or elegant way using the i18n forms.

I didn't found a clear way to doing this, the problems is that the i18n forms in symfony works directly with the $record->Translation, symfony doesn't use any _set method, in this case, there is no modifications in the original record (Subject) only in the recordTranslation object.

Maybe if you override the saveEmbeddedForms method in your record form (SubjectForm), iterating over all the i18n forms and detecting for each one which was modified, and finally modify in some way or flag as modified the original record, so when it is saved the event postSave will be triggered.

public $already_saved;

  public function doSave($con = null) {
    $this->already_saved = $this->object->isModified();
    parent::doSave($con);
  }



public function saveEmbeddedForms($con = null,$forms = null){

if (null === $con)
  $con = $this->getConnection();


if (null === $forms)
  $forms = $this->embeddedForms;

foreach($forms as $form) {
  if  ( count($form->object->isModified()) != 0){ 
    $mark_for_save = true;
    break;
  }
}
parent::saveEmbeddedForms($con, $forms);
if (@$mark_for_save && !$this->already_saved) {
  $this->object->postSave();
}

  }
like image 80
javiertoledos Avatar answered Dec 09 '22 20:12

javiertoledos