How to save related information with Phalcon forms.
I've started using full Phalcon forms as a pose to manually written ones with manually written submits, however I have a question, one of my forms for articles for example has categories, but these are stored seperatley, how can I get these to save to their respective databases?
I have a BlogArticleForm.php with all the form elements defined:
- name
- permalink
- content
- categories // Array of IDs
I have a Blog model, which includes name, permalink and content I also have a Model for Categories which list the different options on the form categories multiselect I then finally have BlogCategories which links categories to blog articles via via id
Using Phalcon form, how can I get the seperate saves to happen using the BlogArticleForm?
Currrently this is what my saveAction looks like:
public function saveAction()
{
$this->view->disable();
if ($this->request->isPost()) {
$form = new BlogArticleForm();
if ($form->isValid($this->request->getPost())) {
$id = $form->get('id')->getValue();
$blog = Blog::findFirstById($id);
$form->bind($this->request->getPost(), $blog);
if ($blog->save()) {
$this->flash->success('Saved changed');
return $this->response->redirect('dashboard/blog/edit/' . $id);
} else {
$this->flash->error('Failed to save changes');
return $this->response->redirect('dashboard/blog/edit/' . $id);
}
} else {
foreach ($form->getMessages() as $message) {
$this->flash->error($message);
}
return $this->response->redirect('dashboard/blog/edit/');
}
}
}