1

I have schema like:

Schema:

 article:
   id:                                      ~
   title:                                   { type: VARCHAR, size: '255', required: true }
   created_at:                              { type: TIMESTAMP, required: true }
   updated_at:                              { type: TIMESTAMP, required: true }

 article_data:
   id:                                      ~
   article_data:                            { type: BLOB, required: true } 
   article_filename:                        { type: VARCHAR, size: '255'}
   article_id:                              { type: INTEGER, required: true, foreignTable: article, foreignReference: id, onDelete: cascade }

So, in my article admin module, I'd like to display the article_data widget, which is a file upload.

Everything is fine. But when saving the uploaded file to the server, the article_id field is null.

Is there a way i could get the id of the article and save it as the article_id of the article_data table?

Thanks

EDIT:

I think I need to override the saveEmbeddedForm() method, but I am not sure what I'd need to do.

Could someone help with some code for a saveEmbeddedForm()?

Thanks

terrid25
  • 1,926
  • 8
  • 46
  • 87

1 Answers1

3

I don't known Propel, but in Doctrine you could do something like this:

class ArticleForm extends BaseForm
{
  public function configure()
  {
    parent::configure();

    $form  = new sfForm();
    $datas = $this->getObject()->getDatas();

    foreach ($datas as $index => $data)
      $form->embedForm($index, new ArticleDataForm($data));

    $this->embedForm('dataForms', $form);
  }

  public function saveEmbeddedForm($con = null, $forms = null)
  {
    $dataForms = $this->getEmbeddedForm('dataForms')->getEmbeddedForms();

    foreach ($dataForms as $dataForm)
      $dataForm->getObject()->setArticle($this->getObject());

    parent::saveEmbeddedForm($con, $forms);
  }
}
joksnet
  • 2,305
  • 15
  • 18
  • Thanks for the reply. I've used your code and modified it a little bit. I used: $this->embedForm('article_data', new ArticleDataForm()); rather than your code in the configure as your code doesn't display the input widget for some reason. Also, even when the input is empty it creates a new row in my table. I need it to only create a new one if there was no image and update the image if editting – terrid25 Jan 14 '11 at 14:59
  • It's fine. I did a one to many relation. You have a one to one relation. Then just do `$this->getEmbeddedForm('article_data')->getObject()->setArticle($this->getObject())` in the `saveEmbeddedForm` function. – joksnet Jan 14 '11 at 17:04