【问题标题】:Symfony2 form collections limitSymfony2 表单集合限制
【发布时间】:2016-01-26 19:04:57
【问题描述】:

我有一个Course 实体,其中有很多CourseSessionsCourseSession 可以激活通过

如何仅渲染活动 CourseSessions 并保存与Course 编辑页面上通过 CourseSessions 的关系?

现在我渲染所有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' =&gt; new CourseSessionType()getActiveCourseSessions()setActiveCourseSessions() 映射?我想这会对我有所帮助。

【问题讨论】:

  • Symfony 没有任何默认的实体“编辑”页面。你在用索纳塔吗?如果是这样,您仍然需要提供有关您正在使用的小部件以及理想情况下相关代码位的更多信息。我猜你需要做的是创建自定义 getActiveCourseSessions 和 setActiveCourseSessions 函数,然后设置奏鸣曲编辑表单呈现“activeCourseSessions”而不是“courseSessions”。
  • “Symfony 没有任何默认的实体‘编辑’页面。”我的意思是课程更新页面 - 来自 CRUD 操作的页面。我肯定在使用 symfony。
  • 您需要提供更多信息。这个问题不包含足够的信息来提供帮助。您是在谈论使用命令“generate:doctrine:crud”生成的代码吗?也许这会对你有所帮助? stackoverflow.com/questions/12497133/…
  • 1) 我手动创建了自定义页面,我可以在其中编辑课程 2) 当课程包含超过 50 个会话(活动和通过)时,我的页面加载速度很慢。问题:如何仅渲染活动会话?附言你到底有什么不明白的,我应该提供什么??

标签: forms symfony


【解决方案1】:

尝试使用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__'
    ));
}

【讨论】:

  • 感谢您的帮助,但我尝试了这种方法,但它不起作用。
  • 您是否也添加了该属性? (private $activeSessions;) 你还需要在构造函数中设置$this-&gt;activeSession = new \Doctrine\Common\Collections\ArrayCollection();
  • 不,我没有。我认为这是一个坏主意,因为对于我们应用程序的其他部分,sessionactive session 之间没有区别,它们是相同的实体。
【解决方案2】:

很高兴告诉您,我找到了解决方案!

我在我的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');
}

我希望它对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    相关资源
    最近更新 更多