减肥Symfony2控制器并保持逻辑

gre*_*emo 7 model-view-controller design-patterns symfony doctrine-orm

从MVC模式和Symfony2的角度来看,我可以稍微减少我的控制器代码,移开一些持久的逻辑吗?例如,给定一个像这样的标准新动作:

public function newAction(\Symfony\Component\HttpFoundation\Request $request)
{

    // Create a new entity and a new form type for managing
    $entity = $this->createNewEntity();
    $form   = $this->createForm($this->createNewFormType(), $entity);

    // If it's GET just return the view
    if('GET' == $request->getMethod())
        return array('form' => $form->createView());

    // It's POST request so bind the form
    $form->bindRequest($request);

    // ... and if it's valid just persist the entity
    if($form->isValid()) :

        $em = $this->getEntityManager(); // Should be carried by controller?
        $em->persist($entity);           // Should be carried by controller?
        $em->flush();                    // Should be carried by controller?

        // Redirect the user to the default page
        return $this->redirect($this->getOnNewSuccessRedirectUrl($entity));

    endif;

    // Return the view plus errors
    return array(
        'errors' => $this->getValidator()->validate($entity),
        'form'   => $form->createView()
    );

}
Run Code Online (Sandbox Code Playgroud)

将该逻辑移动到存储库是否正确?一个例子(警告:可能不起作用):

class BaseRepository extends \Doctrine\ORM\EntityRepository
{

    /**
     * Persist the entity (either new or existent)
     *
     * @param object $entity
     */
    public function save($entity)
    {
        $em = $this->_em;
        $em->persist($entity);
        $em->flush();
    }

    /**
     * Delete the entity.
     *
     * @param object $entity
     */
    public function delete($entity)
    {
        $em = $this->_em;
        $em->remove($entity);
        $em->flush();
    }

}
Run Code Online (Sandbox Code Playgroud)

控制器代码将是:

if($form->isValid()) :

    $this->getRepository()->save($entity);

    // Redirect the user to the default page
    return $this->redirect($this->getOnNewSuccessRedirectUrl($entity));

endif;
Run Code Online (Sandbox Code Playgroud)

Chr*_*nel 11

我认为移动持久性并从控制器中删除逻辑是一个好主意,但存储库不是正确的位置.

来自Symfony2文档(http://symfony.com/doc/current/book/doctrine.html#fetching-objects-from-the-database):

您可以将存储库视为PHP类,其唯一的工作是帮助您获取某个类的实体.

您应该只使用您的存储库类从数据库中获取数据.

我会将你的持久/删除逻辑移动到一个服务中(参见http://symfony.com/doc/current/book/service_container.html),这样你的控制器代码就会像

if($form->isValid()) :

    $this->get('service.entity')->save($entity);

    // Redirect the user to the default page
    return $this->redirect($this->getOnNewSuccessRedirectUrl($entity));

endif;
Run Code Online (Sandbox Code Playgroud)

  • 这是要走的路.执行此类任务的类通常称为实体管理器(例如`MyEntityNameManager`),可以在几个Symfony2包中看到([这里](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Entity/UserManager.php) )和[here](例如https://github.com/sonata-project/SonataAdminBundle/blob/2.0/Model/ModelManagerInterface.php). (3认同)