奏鸣曲管理员 |在表单中创建一对多

Sonata Admin | Create One-To-Many in form

我在尝试创建表单时遇到问题,其他主题对我没有帮助。这就是我想要做的。

这是我的数据库方案的一部分:

现在我想创建一个可以创建课程的表单。在那门课程中,我想创建 多个 会话,我可以在其中 添加日期 附加到它们。它应该是这样的:

在我的课程实体中,我有:

/**
 * @ORM\OneToMany(targetEntity="Session", mappedBy="Course")
 */
private $session;

public function getSession()
{
    return $this->session;
}

在我的会话实体中我有:

/**
 * @var \Studyx\EnrolmentBundle\Entity\Course
 *
 * @ORM\ManyToOne(targetEntity="Studyx\EnrolmentBundle\Entity\Course")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="course_ID", referencedColumnName="ID")
 * })
 */
private $course;

在我的 CourseAdmin class 我有:

->add('session', 'sonata_type_collection', array(
    'type_options' => array(
        // Prevents the "Delete" option from being displayed
        'delete' => false,
        'delete_options' => array(
            // You may otherwise choose to put the field but hide it
            'type'         => 'hidden',
            // In that case, you need to fill in the options as well
            'type_options' => array(
                'mapped'   => false,
                'required' => false,
            )
        )
    )
), array(
    'edit' => 'inline',
    'inline' => 'table',
    'sortable' => 'position'
))

在我的表单中,我看到了按钮 "Add New",但是当我点击它时,出现错误:

Neither the property "session" nor one of the methods "addSession()", "setSession()", "__set()" or "__call()" exist and have public access in class "Name\MyBundle\Entity\Course".

我明白了。但是我怎样才能使这项工作呢?我应该将关系更改为 many-to-many 还是使用其他字段或...?有人可以帮我解决这个问题吗?

更新:

在我的 SessionAdmin 我有:

$formMapper->add('type',      'text',     array('label' => 'Type'));

问题是我真的不知道如何在这个管理表单中连接 session_has_date ...

因为课程和会话之间存在 1..n 关系(一个课程包含多个会话)。这意味着 Course#getSession() 方法没有意义(没有一个会话,有很多会话)。

此外,在表单中添加新会话时,表单组件需要一些方法来添加新会话。包含一个 Course#addSession(Session $session) 方法来执行此操作。没有这种方法,就无法向课程中添加新课时。