【问题标题】:symfony repository logic functionssymfony 存储库逻辑函数
【发布时间】:2017-09-05 16:36:58
【问题描述】:

您好,我看了很多教程,但仍然不知道如何在我已经完成的存储库中制作自定义函数。

这是我的 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(),
    ));
}

我只是想制作一些功能来让控制器更漂亮有什么想法吗?如果您给我和示例,我如何通过自定义存储库功能将我的数据插入数据库。我知道如何进行自定义查询。每个帮助/想法都非常有帮助!

【问题讨论】:

  • 你的动作和你想要的一样漂亮。持久性已经解耦,存储库隐藏了查询的复杂性,你看起来不错,IMO。 :)
  • 是的,我会给出我认为没有问题的查询,但是那些设置器呢?它不好看,不是吗?
  • 我认为这取决于你问谁。顽固的 DDD 粉丝可能希望将其隐藏起来。有些人可能会主张在构造时传递所有参数。我喜欢 Symfony 处理这个问题的方式 - 很明显 - 易于更新 - 易于测试 - 我个人对这种方法没有任何问题。
  • 好吧,我只是觉得如果有人帮我制作自定义函数,它应该会更好看,我只是打电话给$this->saveData(parameters);,一切都完成了
  • 如果你的 Controller 继承了基本的 Symfony Controller(例如Symfony\Bundle\FrameworkBundle\Controller\Controller),你可以简单地做$this->getUser() 而不是通过tokenStorage - 一切看起来都很好。

标签: php symfony repository


【解决方案1】:

来自here

Doctrine 2 ORM 不支持 INSERT via DQLDQL query builder。如需完整语法,请查看the EBNF of DQL

如果你想让你的控制器看起来更漂亮一点,你可以添加更多的抽象,从我的脑海中消失(在 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: 使用本机控制器函数创建表单并获取当前登录的用户。如果您不打算在 $form->isSubmitted()$form->isValid() 之间添加更复杂的逻辑,请使用 $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(),
        ));
    }
}

【讨论】:

    猜你喜欢
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 2012-02-09
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2011-04-02
    相关资源
    最近更新 更多