This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » support » symfony 1.3 and 1.4 » Embed form - how to get id of saved objects.
Embed form - how to get id of saved objects. [message #102978] Fri, 23 July 2010 09:32 Go to next message
tom.lis.  is currently offline tom.lis.
Messages: 78
Registered: January 2009
Location: Lublin
Member
I have build a form with few embed forms for multiple file upload.

class MessageForm extends BaseMessageForm
{
  public function configure()
  {
    $this->unsetAllFieldsExcept(array('parent_id','user_id','subject','body' ));

  for ($i=1; $i<=sfConfig::get('app_max_files_upload'); $i++){
    $FileForm = new FileForm();
    $FileForm->setDefault('user_id', sfContext::getInstance()->getUser()->getGuardUser ()->getId());
    $this->embedForm("file_$i", $FileForm);	
  }
  ...




and then in action when I save the form

  $message	=	$form->save() ;



object are created (message + few files)

And the question is how to retreive ids of saved files ? Or file objects ?

using propel.

here is my schema.yml
#### MESSAGES #####################################################################
  message:
    id: ~
    user_id: { type: integer, foreignTable: sf_guard_user, foreignReference: id, required: false , onDelete: setnull }
    parent_id: { type: integer, foreignTable: message, foreignReference: id, required: false , onDelete: setnull }             
    subject: { type: varchar(1000), required: true}
    body: { type: longvarchar , required: true}    
    is_system_message: { type: boolean, required: true, default: 0 }
    created_at: ~
    updated_at: ~  

  message_attachment:
    id:
    message_id:     { type: integer, foreignTable: message, foreignReference: id, required: true, onDelete: cascade }
    file:  { type: varchar(500), required: true}  

  message_user:    
    id: ~
    message_id:     { type: integer, foreignTable: message, foreignReference: id, required: true, onDelete: cascade }
    user_id:     { type: integer, foreignTable: sf_guard_user, foreignReference: id, required: true, onDelete: cascade }
    is_read: { type: boolean, required: true, default: 0 }   

#### FILES #####################################################################
  file:
    id: ~
    user_id: { type: integer, foreignTable: sf_guard_user, foreignReference: id, required: false , onDelete: setnull }
    file:  { type: varchar(500), required: true}
    created_at: ~
    updated_at: ~  

  file_user:    
    id: ~
    file_id:     { type: integer, foreignTable: message, foreignReference: id, required: true, onDelete: cascade }
    user_id:     { type: integer, foreignTable: sf_guard_user, foreignReference: id, required: true, onDelete: cascade }


Re: Embed form - how to get id of saved objects. [message #103032 is a reply to message #102978 ] Sat, 24 July 2010 15:41 Go to previous message
tom.lis.  is currently offline tom.lis.
Messages: 78
Registered: January 2009
Location: Lublin
Member
I think I have made one step forward.

$arrFiles	=	array ();			
foreach ($form->getEmbeddedForms() as $embed_form){
  array_push ($arrFiles, $embed_form->save () ) ;					 
}			
			
$message	=	$form->save(); 
//do something with $arrFiles



but this piece of code do not wrok as I expected.
It throws a validatorSchema exception.

And one more thing about saving embedded form in the foreach loop:
$embed_form->isValid() - return false 
$embed_form->getValues() - return array()







500 | Internal Server Error | sfValidatorErrorSchema
stack trace

    * at ()
      in SF_ROOT_DIR/lib/form/base/BaseFileForm.class.php line 34 ...
              31.
              32.     $this->widgetSchema->setNameFormat('file[%s]');
              33.
              34.     $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
              35.
              36.     parent::setup();
              37.   }
    * at BaseFileForm->setup()
      in SF_ROOT_DIR/lib/vendor/symfony/lib/form/sfForm.class.php line 69 ...
              66.     $this->widgetSchema    = new sfWidgetFormSchema();
              67.     $this->errorSchema     = new sfValidatorErrorSchema($this->validatorSchema);
              68.
              69.     $this->setup();
              70.     $this->configure();
              71.
              72.     $this->addCSRFProtection($this->localCSRFSecret);
    * at sfForm->__construct(array(), array(), null)
      in SF_ROOT_DIR/lib/vendor/symfony/lib/form/addon/sfFormSymfony.class.php line 33 ...
              30.    */
              31.   public function __construct($defaults = array(), $options = array(), $CSRFSecret = null)
              32.   {
              33.     parent::__construct($defaults, $options, $CSRFSecret);
              34.
              35.     if (self::$dispatcher)
              36.     {
    * at sfFormSymfony->__construct(array(), array(), null)
      in SF_ROOT_DIR/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/form/sfFormPropel.class.php line 50 ...
              47.       $this->isNew = $this->getObject()->isNew();
              48.     }
              49.
              50.     parent::__construct(array(), $options, $CSRFSecret);
              51.
              52.     $this->updateDefaultsFromObject();
              53.   }
    * at sfFormPropel->__construct()
      in SF_ROOT_DIR/lib/form/MessageForm.class.php line 19 ...
              16.         
              17.         $arrFiles    =    array ();
              18.         for ($i=1; $i<=sfConfig::get('app_max_files_upload'); $i++){
              19.             $FileForm = new FileForm();
              20.             $FileForm->setDefault('user_id', sfContext::getInstance()->getUser()->getGuardUser ()->getId());
              21.             $this->embedForm("plik_$i", $FileForm);    
              22.         }
    * at MessageForm->configure()
      in SF_ROOT_DIR/lib/vendor/symfony/lib/form/sfForm.class.php line 70 ...
              67.     $this->errorSchema     = new sfValidatorErrorSchema($this->validatorSchema);
              68.
              69.     $this->setup();
              70.     $this->configure();
              71.
              72.     $this->addCSRFProtection($this->localCSRFSecret);
              73.     $this->resetFormFields();
    * at sfForm->__construct(array(), array(), null)
      in SF_ROOT_DIR/lib/vendor/symfony/lib/form/addon/sfFormSymfony.class.php line 33 ...
              30.    */
              31.   public function __construct($defaults = array(), $options = array(), $CSRFSecret = null)
              32.   {
              33.     parent::__construct($defaults, $options, $CSRFSecret);
              34.
              35.     if (self::$dispatcher)
              36.     {
    * at sfFormSymfony->__construct(array(), array(), null)
      in SF_ROOT_DIR/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/form/sfFormPropel.class.php line 50 ...
              47.       $this->isNew = $this->getObject()->isNew();
              48.     }
              49.
              50.     parent::__construct(array(), $options, $CSRFSecret);
              51.
              52.     $this->updateDefaultsFromObject();
              53.   }
    * at sfFormPropel->__construct()
      in SF_ROOT_DIR/apps/frontend/modules/message/actions/actions.class.php line 61 ...
              58.     {
              59.         $this->forward404Unless($request->isMethod(sfRequest::POST));
              60.
              61.         $this->form = new MessageForm();
              62.
              63.         $this->custom_params['from']  = $request->getParameter('from', 'messages');
              64.         $this->custom_params['page']  = $request->getParameter('page', '1');  
    * at messageActions->executeCreate(object('sfWebRequest'))
      in SF_ROOT_DIR/lib/vendor/symfony/lib/action/sfActions.class.php line 60 ...
              57.     }
              58.
              59.     // run action
              60.     return $this->$actionToRun($request);
              61.   }
              62. }
  63.
    * at sfActions->execute(object('sfWebRequest'))
      in SF_ROOT_DIR/lib/vendor/symfony/lib/filter/sfExecutionFilter.class.php line 92 ...
              89.   {
              90.     // execute the action
              91.     $actionInstance->preExecute();
              92.     $viewName = $actionInstance->execute($this->context->getRequest());
              93.     $actionInstance->postExecute();
              94.
              95.     return null === $viewName ? sfView::SUCCESS : $viewName;
    * at sfExecutionFilter->executeAction(object('messageActions'))
Previous Topic:SOLVED - NEWBIE routing question
Next Topic:Problem with i18n default_culture
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software