简体   繁体   中英

Sonata Admin | Create One-To-Many in form

I'm having a problem with a form I'm trying to create and the other topics couldn't help me. This is what I'm trying to do.

This is a part of my database scheme :

在此处输入图片说明

Now I would like to create a form where I can create a course. In that course I want to create multiple sessions where I can add dates attached to them. It should be something like this:

在此处输入图片说明

In my Course Entity I have:

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

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

In my Session Entity I have:

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

In my CourseAdmin class I have:

->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'
))

In my form I see the button "Add New" but when I click on it I get the error:

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".

I get this. But how can I make this work? Should I change the relation to many-to-many or use another field or ... ? Can someone help me with this?

UPDATE:

In my SessionAdmin I have:

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

The problem is I don't really know how to couple the session_has_date in this Admin form ... .

As there is a 1..n relation between Course and Session (one course contains many sessions). This means that a Course#getSession() method does not make sense (there is not one sessions, there are many sessions).

Also, when adding new sessions in a form, the Form component needs some way to add new sessions. Include a Course#addSession(Session $session) method to do this. Without this method, there is no way to add new sessions to a course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM