1

Hello i saw a lot of tutorials and still dont know how can i make custom functions in my already done repository.

this is content of my CommentController:

public function newAction(Request $request, $productId)
{
    $comment = new Comment();
    $form = $this->formFactory->create(CommentForm::class, $comment);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        $em = $this->get('doctrine')->getManager();
        /** @var Product $product */
        $product = $em->getRepository(Product::class)->find($productId);

        $comment->setAuthor($this->tokenStorsge->getToken()->getUser());
        $comment->setProduct($product);
        $comment->setContent($form->get('content')->getData());

        $this->em->persist($comment);
        $this->em->flush();

    }

    return $this->render(':form:comment.html.twig', array(
        'form' => $form->createView(),
    ));
}

and i just want to make some function to make controller more beautiful any ideas? if you give me and example how can i insert my data into database via custom repository function. I know how to make custom query thats all. Every help/idea is very helpfull!

Jakub Staněk
  • 65
  • 1
  • 9
  • 1
    Your action is about as beautiful as one would want it. Persistence is already decoupled, repository hide the complexity of queries, your looking good, IMO. :) – Alex.Barylski Sep 05 '17 at 16:49
  • Yes i would give queries that is not problem i guess, but what about that setters? Its not nice, does not it? – Jakub Staněk Sep 05 '17 at 16:52
  • I think it depends on who you ask. Die hard DDD fans might want that tucked away. Some might advocate passing all parameters at construction. I like the way Symfony handles this - it's clear - easy to update - easy to test - I have no issues with this approach personally. – Alex.Barylski Sep 05 '17 at 16:57
  • Okey i just thought it should look better if somebody help me make custom function and i just call something like `$this->saveData(parameters);` and everything is done – Jakub Staněk Sep 05 '17 at 17:01
  • If your Controller inherits the base Symfony Controller (eg. `Symfony\Bundle\FrameworkBundle\Controller\Controller`), you can simply do `$this->getUser()` instead of going through the tokenStorage - everything else seems fine. – ccKep Sep 05 '17 at 17:03
  • Okey guys, thanks:) – Jakub Staněk Sep 05 '17 at 17:07
  • If you use Symfony 3.3 you can inject services in your controller actions with type-hinting. See https://symfony.com/doc/current/service_container/autowiring.html – jrswgtr Sep 05 '17 at 19:05

1 Answers1

1

From here

Doctrine 2 ORM does not support INSERT via DQL or the DQL query builder. For a complete syntax, check the EBNF of DQL.

You can add more abstractions if you want your controller to look slightly more beautiful, off the top of my head (effective in Symfony 2.8):

BaseController.php:

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

abstract class BaseController extends Controller
{
    /**
     * @return ProductRepository
     */
    protected function getProductRepo()
    {
        return $this->get('doctrine.orm.entity_manager')->getRepository(Product::class);
    }
}

CommentController.php: Use native controller functions to create a form and get the currently logged in user. And if you are not intend to add more complex logic between $form->isSubmitted() and $form->isValid() use just $form->isValid()

class CommentController extends BaseController
{
    public function newAction(Request $request, $productId)
    {
        $comment = new Comment();

        $form = $this->createForm(CommentForm::class, $comment);
        $form->handleRequest($request);

        if ($form->isValid()) {
            /** @var Product $product */
            $product = $this->getProductRepo()->find($productId);

            $comment->setAuthor($this->getUser());
            $comment->setProduct($product);
            $comment->setContent($form->get('content')->getData());

            $this->em->persist($comment);
            $this->em->flush();

        }

        return $this->render(':form:comment.html.twig', array(
            'form' => $form->createView(),
        ));
    }
}
genesst
  • 1,333
  • 1
  • 10
  • 39