Php Sonata Admin |在表单中创建一对多

Php Sonata Admin |在表单中创建一对多,php,symfony,doctrine-orm,sonata-admin,sonata,Php,Symfony,Doctrine Orm,Sonata Admin,Sonata,我试图创建的表单有问题,其他主题无法帮助我。这就是我想做的 这是我的数据库方案的一部分: 现在我想创建一个表单,在这里我可以创建一个课程。在该课程中,我想创建多个会话,在这些会话中我可以添加日期。应该是这样的: 在我的课程实体中我有: /** * @ORM\OneToMany(targetEntity="Session", mappedBy="Course") */ private $session; public function getSession() { return

我试图创建的表单有问题,其他主题无法帮助我。这就是我想做的

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

现在我想创建一个表单,在这里我可以创建一个课程。在该课程中,我想创建多个会话,在这些会话中我可以添加日期。应该是这样的:

在我的课程实体中我有:

/**
 * @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;
->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'
))
$formMapper->add('type',      'text',     array('label' => 'Type'));
在我的会话实体中我有:

/**
 * @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;
->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'
))
$formMapper->add('type',      'text',     array('label' => 'Type'));
在我的课程中,我有:

/**
 * @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;
->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'
))
$formMapper->add('type',      'text',     array('label' => 'Type'));
在我的表单中,我看到了“添加新”按钮,但当我单击该按钮时,会出现错误:

属性“session”或方法“addSession()”、“setSession()”、“\uuu set()”或“\uu call()”都不存在,并且在类“Name\MyBundle\Entity\Course”中具有公共访问权限

我明白了。但是我怎样才能让它工作呢?我应该将关系更改为多对多还是使用其他字段或?有人能帮我吗

更新:

在mySessionAdmin中,我有:

/**
 * @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;
->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'
))
$formMapper->add('type',      'text',     array('label' => 'Type'));

问题是我真的不知道如何在这个管理表单中耦合会话日期

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


另外,在表单中添加新会话时,表单组件需要某种方式来添加新会话。包括一个
课程#添加会话(会话$Session)
方法来完成此操作。如果没有此方法,就无法向课程中添加新课程。

您可以尝试使用
by\u reference=>false
选项吗?您有会话目标类的管理类吗?@chalasr:by_reference=>false尝试过,但错误相同。使用会话管理类更新了我的主题。