Sonata Admin Bundle:空实体引用在实体创建期间抛出异常

时间:2017-08-17 08:43:40

标签: symfony sonata-admin symfony-sonata symfony-3.3

我是Sonata Admin Bundle的新手,所以这可能是基本的。我有一个实体StudentCourse

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Id;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Table(name="student_course")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\StudentCourseRepository")
 *
 * Class StudentCourse
 */
class StudentCourse
{
    /**
     * @Id
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Student", inversedBy="studentCourses")
     * @Assert\NotBlank()
     */
    private $student;
    /**
     * @Id
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Course", inversedBy="studentCourses")
     * @Assert\NotBlank()
     */
    private $course;

    /**
     * @ORM\Column(type="datetime")
     * @Assert\NotBlank()
     */
    private $startedAt;


    /**
     * @return mixed
     */
    public function getStartedAt()
    {
        return $this->startedAt;
    }

    /**
     * @param mixed $startedAt
     */
    public function setStartedAt($startedAt)
    {
        $this->startedAt = $startedAt;
    }

    /**
     * @return mixed
     */
    public function getStudent() {
        return $this->student;
    }

    /**
     * @param mixed $student
     */
    public function setStudent($student) {
        $this->student = $student;
    }

    /**
     * @return mixed
     */
    public function getCourse() {
        return $this->course;
    }

    /**
     * @param mixed $course
     */
    public function setCourse($course) {
        $this->course = $course;
    }
}

StudentCourseAdmin

namespace AppBundle\Admin;

use AppBundle\Entity\Course;
use AppBundle\Entity\Student;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\CoreBundle\Validator\ErrorElement;

class StudentCourseAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper->add('student', 'entity', [
            'class' => Student::class,
            'choice_label' => 'lastName',
            'required' => true,
        ]);
        $formMapper->add('course', 'entity', [
            'class' => Course::class,
            'choice_label' => 'name',
            'required' => true,
        ]);
        $formMapper->add('startedAt', 'date', [
            'required' => true,
        ]);
    }

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        //$datagridMapper->add('student');
        //$datagridMapper->add('course');
    }

    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper->add('student');
        $listMapper->add('course');
        $listMapper->add('startedAt');
    }

    /**
     * {@inheritdoc}
     */
    public function validate(ErrorElement $errorElement, $object)
    {
        $errorElement
            ->with('student')
                ->assertNotBlank()
                ->assertNotNull()
            ->end()
            ->with('course')
                ->assertNotBlank()
                ->assertNotNull()
            ->end();

    }
}

在服务中:

app.admin.student_course:
    class: AppBundle\Admin\StudentCourseAdmin
    arguments: [~, AppBundle\Entity\StudentCourse, ~]
    tags:
        - { name: sonata.admin, manager_type: orm, label: StudentCourse }
    public: true

没有Course可用。当我尝试使用空StudentCourse字段创建实体Course时,我收到了异常。这是有点期待的,而不是预期的 - 我不明白为什么没有对此进行验证,只是残酷的例外 - 我错过了什么?如何更优雅地处理这种情况?

我正在使用Symfony 3.3.6和Sonata Admin Bundle 3.21.0

我得到的例外是:

  

在渲染模板期间抛出了异常   ("注意:未定义索引:course_id")in   " SonataAdminBundle:CRUD:base_edit_form.html.twig"

1 个答案:

答案 0 :(得分:0)

我不知道为什么你为可以用ManyToMany处理的东西创建了一个实体。但你的问题可能就在这里:

/**
 * @Id
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Course", inversedBy="studentCourses")
 *** @Assert\NotBlank()** <---- here !!!
 */
private $course;

试试这个:

    /**
     * @Id
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Course", inversedBy="studentCourses")
     * @ORM\JoinColumn(name="course_id", referencedColumnName="id", nullable= true)
     */
    private $course;

之后,您必须通过控制台使用以下命令更新架构:

php bin/console doctrine:schema:update --force
你可能想看看这个: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html