2012-05-10 14 views
7

を持続?例えば、このような特定の標準的な新しいアクション:スリミングダウンSymfony2のコントローラと私は、私の<strong>コントローラのコード</strong>少しだけスリム化することができますいくつかの<strong>は論理</strong>を持続離れる、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() 
    ); 

} 

は、リポジトリにそのロジックを動かす正しいだろうか?例(警告:動作しない場合があります):

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(); 
    } 

} 

コントローラコードは次のようになります。

if($form->isValid()) : 

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

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

endif; 
+0

$ em-> flush($ entity)を呼び出します。 $ em-> flush()の代わりに。あなたは与えられたエンティティに対してのみ変更を行い、サブエンティティは関連しています。 –

答えて

10

私はそれはあなたのコントローラのうち、あなたの存続と削除ロジックを移動するには良いアイデアだと思うが、リポジトリが正しい場所ではありません。

あなたが唯一の仕事あなたが特定のクラスの実体を取得支援することであるPHPクラスとしてリポジトリと考えることができます:Symfony2のドキュメント(http://symfony.com/doc/current/book/doctrine.html#fetching-objects-from-the-database)から

データベースからデータを取得する場合にのみ、リポジトリクラスを使用する必要があります。

私はあなたのコントローラコードが

if($form->isValid()) : 

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

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

endif; 
+3

それは行く方法です。このようなタスクを実行するクラスは通常エンティティマネージャ(MyEntityNameManagerなど)と呼ばれ、いくつかのSymfony2バンドル([ここ](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Entity/UserManager.php)で見ることができます。 )と[ここ](例:https://github.com/sonata-project/SonataAdminBundle/blob/2.0/Model/ModelManagerInterface.php)を参照してください)。 – gilden

+0

私はこれが古かったことを知っていますが、Symfonyがこのパターンを推薦した理由は不思議です。 Entity FrameworkとHibernateを使用するプロジェクトでは、通常リポジトリ内の永続ロジックが表示されます。どんな考え? 私はすべてSOAですが、各エンティティが永続ロジックを処理するための新しいサービスを作成するのは冗長なようです。 –

4

クリスが絶対的に正しいですようなものになるだろう(http://symfony.com/doc/current/book/service_container.htmlを参照)サービスにロジックを削除し、あなたが/持続動くでしょう。

しかし、方法によって

は、なぜあなたは、このように再びあなたのエンティティを検証します:

'エラー' =>の$ this - > getValidator() - >検証($エンティティ)?

フォームにリクエストをバインドするので、エラーはビューヘルパーのform_errors(...)で作成されたビューで処理されます。

+0

あなたの権利、それはまったく役に立たなかった、ありがとう。 +1 – gremo

関連する問題

 関連する問題