Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Save related items with Phalcon form

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/');
            }
        }
    }



830

Yes, but that doesn't exactly answer my question about how it knows to bind the related items.

you may use many-to-many db relational, and use this to saving https://docs.phalconphp.com/en/latest/reference/models.html#storing-related-records

It's just a guess based on the source, but you should probably implement a setter on your model, for eg:

class MyModel extends Phalcon\Model
{
    public function setHasOneData($data) {
        $entity = new HasOneModel();
        $entity->assign($data);
        $this->HasOne = $entity;
    }
    public function setHasManyData($data) {
        $items = [];
        foreach($data as $d) {
            $entity = new HasManyModel();
            $entity->assign($d);
            $items[] = $entity;
        }
        $this->HasMany = $items;
    }
}

And then in your form, name your input following that logic, for eg: has_one_data and has_many_data



830
Accepted
answer

I think you try to use this from form viewside, so Phalcon cannot save nested Form, to workaround this, you need to create 2 form, and bind it, and attached it to parent Model manually after validated