EasyAdmin中收集类型的插入问题

发布于 2025-02-02 17:45:25 字数 4283 浏览 3 评论 0原文

我和我的Collectionfield有一个问题。 我解释我的问题。我想创建一个工作,我们可以添加一个或多个任务。每个任务都可以有几个活动等。当我插入2个以上的任务时, 每个theings

但是我有两个问题:

执行查询时发生了一个例外:sqlstate [23000]:完整性约束违规:1048列“名称”不能为null

这是我的代码 jobcrudcontroller

<?php

namespace App\Controller\Admin;

use App\Entity\Job;
use App\Form\TaskType;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;

class JobCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Job::class;
    }


    public function configureFields(string $pageName): iterable
    {
        return [
            AssociationField::new('sector', 'Secteur'),
            TextField::new('name', 'Nom du métier'),
            CollectionField::new('tasks', 'Tâche(s)')
                ->setEntryType(TaskType::class),
         ];
    }

    public function configureCrud(Crud $crud): Crud
    {
        return $crud
            ->setDefaultSort([
                'sector.name' => 'ASC',
                'name' => 'ASC',
            ])
            ->setEntityLabelInSingular('Métier')
            ->setEntityLabelInPlural('Métiers');
    }
}

内部 tasktype

<?php

namespace App\Form;

use App\Entity\Task;
use App\Form\ActivityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;

class TaskType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', TextType::class, [
                'label' => 'Nom de la tâche',
            ])
            ->add('activities', CollectionType::class, [
                'label' => 'Activité(s)',
                'entry_type' => ActivityType::class,
                'allow_add' => true,
                'allow_delete' => true,
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Task::class,
        ]);
    }
}

and strong>和< Strong> ActivityType :

<?php

namespace App\Form;

use App\Entity\Activity;
use App\Form\UnderActivityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;

class ActivityType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', TextType::class, [
                'label' => 'Nom de l\'activité',
            ])
            ->add('underActivities', CollectionType::class, [
                'label' => 'Sous-Activité(s)',
                'entry_type' => UnderActivityType::class,
                'allow_add' => true,
                'allow_delete' => true,
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Activity::class,
        ]);
    }
}

我不知道如何解决这个问题,您有任何解决方案吗? 诺

I got an issue with my CollectionField.
I explain my problem. I want to create a job, for which we can add one or more tasks. Each task can have several activities etc. When I insert 2+ tasks,
everythings works.

But I have 2 problems:

An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null

Here is my code inside JobCrudController :

<?php

namespace App\Controller\Admin;

use App\Entity\Job;
use App\Form\TaskType;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;

class JobCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Job::class;
    }


    public function configureFields(string $pageName): iterable
    {
        return [
            AssociationField::new('sector', 'Secteur'),
            TextField::new('name', 'Nom du métier'),
            CollectionField::new('tasks', 'Tâche(s)')
                ->setEntryType(TaskType::class),
         ];
    }

    public function configureCrud(Crud $crud): Crud
    {
        return $crud
            ->setDefaultSort([
                'sector.name' => 'ASC',
                'name' => 'ASC',
            ])
            ->setEntityLabelInSingular('Métier')
            ->setEntityLabelInPlural('Métiers');
    }
}

Inside TaskType

<?php

namespace App\Form;

use App\Entity\Task;
use App\Form\ActivityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;

class TaskType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', TextType::class, [
                'label' => 'Nom de la tâche',
            ])
            ->add('activities', CollectionType::class, [
                'label' => 'Activité(s)',
                'entry_type' => ActivityType::class,
                'allow_add' => true,
                'allow_delete' => true,
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Task::class,
        ]);
    }
}

And ActivityType :

<?php

namespace App\Form;

use App\Entity\Activity;
use App\Form\UnderActivityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;

class ActivityType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', TextType::class, [
                'label' => 'Nom de l\'activité',
            ])
            ->add('underActivities', CollectionType::class, [
                'label' => 'Sous-Activité(s)',
                'entry_type' => UnderActivityType::class,
                'allow_add' => true,
                'allow_delete' => true,
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Activity::class,
        ]);
    }
}

I don't know how to solve this, do you have any solutions?
Noé

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文