2012-05-10 32 views
7

Từ quan điểm của mẫu MVC và Symfony2, tôi có thể giảm bớt mã điều khiển chỉ một chút, di chuyển một số logic tiếp tục? Ví dụ, cho một hành động mới tiêu chuẩn như thế này:Giảm bớt bộ điều khiển Symfony2 và tiếp tục logic

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

} 

Sẽ là đúng di chuyển mà logic để kho? Một ví dụ (cảnh báo: có thể không hoạt động):

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

} 

Mã điều khiển sẽ là:

if($form->isValid()) : 

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

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

endif; 
+0

Bằng cách gọi $ em-> flush ($ entity); thay vì $ em-> flush(); bạn chỉ đang thực hiện các thay đổi cho thực thể đã cho và liên quan các thực thể con. –

Trả lời

10

Tôi nghĩ rằng đó là một ý tưởng tốt để di chuyển của bạn tồn tại và loại bỏ logic ra khỏi kiểm soát của bạn, nhưng kho lưu trữ không phải là địa điểm chính xác cho nó.

Từ các tài liệu Symfony2 (http://symfony.com/doc/current/book/doctrine.html#fetching-objects-from-the-database):

Bạn có thể nghĩ về một kho lưu trữ như là một lớp PHP mà công việc duy nhất là để giúp bạn lấy thực thể của một lớp nào đó.

Bạn chỉ nên sử dụng lớp lưu trữ của mình để tìm nạp dữ liệu từ cơ sở dữ liệu.

tôi sẽ di chuyển của bạn kéo dài/loại bỏ logic thành một dịch vụ (xem http://symfony.com/doc/current/book/service_container.html) do đó, mã điều khiển của bạn sẽ là một cái gì đó giống như

if($form->isValid()) : 

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

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

endif; 
+3

Đó là con đường để đi. Các lớp thực hiện các tác vụ như vậy thường được gọi là quản lý thực thể (ví dụ 'MyEntityNameManager') và có thể thấy trong một số gói Symfony2 ([ở đây] (https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Entity/UserManager.php) và [ở đây] (https://github.com/sonata-project/SonataAdminBundle/blob/2.0/Model/ModelManagerInterface.php) chẳng hạn). – gilden

+0

Tôi biết điều này là cũ nhưng tôi tự hỏi tại sao Symfony đi với đề xuất mô hình này. Bạn sẽ thường thấy logic tồn tại trong kho lưu trữ trong các dự án sử dụng Entity Framework và Hibernate. Bất kỳ ý tưởng tại sao? Tôi là tất cả cho SOA nhưng có vẻ như dư thừa để tạo ra một dịch vụ mới cho mỗi thực thể để xử lý logic tồn tại. –

4

Chris là hoàn toàn đúng.

Nhưng bằng cách này:

Tại sao bạn xác nhận bạn thực thể một lần nữa theo cách này:

'lỗi' => $ this-> getValidator() -> xác nhận ($ tổ chức)?

Bạn ràng buộc yêu cầu vào biểu mẫu, do đó các lỗi được xử lý trong chế độ xem được tạo trong biểu mẫu form_errors (...) 'của người trợ giúp.

+0

Quyền của bạn, điều đó hoàn toàn vô dụng, cảm ơn. +1 – gremo

Các vấn đề liên quan