2016-01-26 116 views
-1

我有一個Course實體至極有許多CourseSessionsCourseSession可以活性傳遞Symfony2的形式集合限制

我怎麼能只渲染活躍CourseSessions並與通過CourseSessionsCourse編輯頁面上保存的關係?

現在我渲染所有CourseSessions但是當CourseSession數超過50個時,頁面呈現速度很慢。

任何人都可以幫助我嗎? 謝謝。

更新1:

class CourseType { 
    $builder->add('sessions', 'collection', array(
     'label' => false, 
     'type' => new CourseSessionType(), // <- here I want to pass only active Sessions 
     'allow_add' => true, 
     'allow_delete' => true, 
     'prototype' => true, 
     'prototype_name' => '__name__' 
    )) 
} 

class CourseSessionType { 
    // multiple CourseSession fields 
} 

// Course edit page 
<div id="courseSessions" data-prototype="{{macros.course_session_prototype(form.sessions, 'Remove Session', true)|escape }}"> 
    {% do form.sessions.setRendered %} 
    {% for widget in form.sessions.children %} 
     {{ macros.course_session_prototype(widget, 'Remove Session', false) }} 
    {% endfor %} 
</div> 

更新2:

我怎麼能映射我'type' => new CourseSessionType()getActiveCourseSessions()setActiveCourseSessions()?我認爲這會對我有幫助。

+0

Symfony的不具有實體的任何默認的「編輯」頁面。你在使用索納塔嗎?如果是這樣,你仍然需要提供更多關於你正在使用的小部件的信息,理想情況下還需要提供相關的代碼部分。我會猜,你需要做的是創建自定義getActiveCourseSessions和setActiveCourseSessions功能,然後設置已奏鳴曲編輯形式呈現,而不是「courseSessions」 activeCourseSessions「。 – Omn

+0

「Symfony沒有任何實體的默認'編輯'頁面。」我的意思是課程更新頁面 - 一個來自CRUD操作。我確實使用了symfony。 –

+0

您需要提供更多信息。這個問題沒有足夠的信息來幫助。你在談論使用命令「generate:doctrine:crud」生成的代碼嗎?也許這會幫助你? http://stackoverflow.com/questions/12497133/directly-access-a-form-fields-value-when-overriding-widget-in-a-twig-template – Omn

回答

0

我glat告訴你,我找到了解決方案!

我在Course實體$showOnlyActiveSessions屬性添加和更改getSessions()方法

class Course 
{ 
    /** 
    * Show only active sessions flag 
    * @var bool 
    */ 
    private $showOnlyActiveSessions = false; 

    // other properties 

    /** 
    * Get course sessions 
    * @return CourseSession [] 
    */ 
    public function getSessions() 
    { 
     $sessions = $this->sessions; 
     if ($this->getShowOnlyActiveSessions()) { 
      return array_filter($sessions->toArray(), function($session) { 
       return !$session->isPast(); 
      }); 
     } 
     return $this->sessions; 
    } 

    /** 
    * @return bool 
    */ 
    public function getShowOnlyActiveSessions() 
    { 
     return $this->showOnlyActiveSessions; 
    } 

    /** 
    * @param bool $boolValue 
    */ 
    public function setShowOnlyActiveSessions($boolValue) 
    { 
     $this->showOnlyActiveSessions = $boolValue; 
    } 
} 

而現在,當我需要得到的只是活動會話我做我的控制器中的以下內容:

public function editAction(Request $request, Course $course) 
{ 
    $course->setShowOnlyActiveSessions(true); 

    $editForm = $this->createEditForm($course); 
    $editForm->handleRequest($request); 

    if ($editForm->isValid()) { 
     // handle and persist form 
    } 
    return $this->redirectToRoute('course_list'); 
} 

我希望這會對某人有所幫助。

0

嘗試將字段的名稱設置相應的方法使用active_course_sessions或者更短,如果你的名字你的方法Course::getActiveSessions()Course::setActiveSessions()這是可以做到這樣的映射:

class CourseType { 
    $builder->add('active_sessions', 'collection', array(
     'label' => false, 
     'type' => new CourseSessionType(), 
     'allow_add' => true, 
     'allow_delete' => true, 
     'prototype' => true, 
     'prototype_name' => '__name__' 
    )); 
} 
+0

感謝您的幫助,但我嘗試了這種方法,但無效。 –

+0

你還加了房產嗎?('private $ activeSessions;')您還需要在構造函數中設置'$ this-> activeSession = new \ Doctrine \ Common \ Collections \ ArrayCollection();' – Heah

+0

不,我沒有。我認爲這是一個壞主意,因爲對於我們的應用程序的其他部分而言,'session'和'active session'之間沒有區別,它們是相同的實體。 –