Skip to content

Instantly share code, notes, and snippets.

@tacone
Created September 30, 2014 20:24
Show Gist options
  • Save tacone/3f8bb8c4e8e57ae42585 to your computer and use it in GitHub Desktop.
Save tacone/3f8bb8c4e8e57ae42585 to your computer and use it in GitHub Desktop.
Rapyd scratchpad
<?php
function edit ()
{
$model = new Article();
$form = new DataForm($model); //--> $form->model
$form->text('title');
$form->text('author.name');
$form->file('cover')->move('/uploads', 'cover{{$id}}');
/*
$this->form->afterSave(function($form, $caller){
if (!$caller->moveFile())
{
// rollback DB transaction
throw new FileUploadException('file could not be uploaded');
}
});
// nel caso ci fossero più file
$this->form->onRollback(function($form, $caller){
$caller->rollbackUpload();
});
*/
if (Request::method == 'POST')
{
$data = Input::all();
$data = array_dot($data);
foreach ( $data as $key => $value)
{
$form->set($key, $value); // --> array_set($form->model, $key, $value);
}
if ($form->validate())
{
$form->save();
/*
// begin transaction
try {
$this->model->save();
foreach ($this->getRelations() as $submodel)
{
$submodel->save();
}
} catch ($exception)
{
$this->rollback();
}
// commit transaction
*/
$form->redirect(); // to some page
}
}
View::make('edit', compact('form'));
}
@zofe
Copy link

zofe commented Oct 1, 2014

ok, le cose piu' concrete da mettere in mezzo mi sembrano..

  • array_dot per fare il match fra l'array del post e i nomi dei campi
  • array_get / array_fetch per prendere dall'array multidimensionale solo il livello che ci interessa lavorare (per salvare rispettivamente un relazionato o un elenco di relazionati)

@tacone
Copy link
Author

tacone commented Oct 1, 2014

Sto cercando di fare il quadro di come dovrebbe funzionare la cosa, c'è validazione, redirect, upload dei files, transazioni.

C'è altra roba base che mi sono dimenticato?

@zofe
Copy link

zofe commented Oct 1, 2014

Io le transazioni non le metterei in mezzo, tutto cio' che puo' essere gestito da framework e che è specifico per l'applicazione che stai realizzando è giusto che sia nei model o nel controller.. es.

https://github.com/zofe/rapyd-laravel/blob/master/src/models/Article.php#L42

o in questo caso qualcosa nel controller tipo..

DB::transaction(function() use ($form) 
{
    $form->save(); 
});

@tacone
Copy link
Author

tacone commented Oct 1, 2014

creano problemi?

@zofe
Copy link

zofe commented Oct 2, 2014

non credo, dicevo semplicemente che si possono usare lo stesso senza metterle nel dataform.
Se si tratta di aggiungere qualche metodo facciamolo pure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment