Sylius: Add Factory Method for custom form with relationship?

Created on 7 Sep 2016  Â·  3Comments  Â·  Source: Sylius/Sylius

I'm trying to save a form of my comment entity. Comment entity is related with post entity.

I can't preset the ID of the post in the CommentType then I read in the docs about to create a custom Factory Method. But is not explained how to create, just how to configure. I think about i need make one method to take the Post Slug's and save when the form is send.

Help Wanted

Most helpful comment

We can move this conversation to stackoverflow. I have posted some brief explantation of factory decoration there.

Probably can be closed.

All 3 comments

I was looking into the code of your bundles and I find an example.

I made my own Factory and FactoryInterface.

My Factory looks like this:

class CommentFactory implements CommentFactoryInterface
{
    /**
     * @var FactoryInterface
     */
     private $factory;

    /**
     * @param FactoryInterface $factory
     */
    public function __construct(FactoryInterface $factory)
    {
        $this->factory = $factory;
    }

    /**
     * {@inheritDoc}
     */
    public function createNew()
    {
        return $this->factory->createNew();
    }

    public function createWithPost($postId)
    {
        $comment = $this->createNew();
        $comment->setPost($postId);

        return $comment;
    }
}

Now I can't find how to inject the FactoryInterface of Sylius ResourceBundle.

Any tip?

comment_create:
    path: /new/{postId}
    methods: [GET, POST]
    defaults:
        _controller: acme.controller.comment:createAction
        _sylius:
            template: "..."
            factory:
                method: createWithPost
                arguments: [$postId]

Does that work for you in routing?

We can move this conversation to stackoverflow. I have posted some brief explantation of factory decoration there.

Probably can be closed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

inssein picture inssein  Â·  3Comments

xleliberty picture xleliberty  Â·  3Comments

eb22fbb4 picture eb22fbb4  Â·  3Comments

crbelaus picture crbelaus  Â·  3Comments

foxou33 picture foxou33  Â·  3Comments