php – 为什么我的关系不想工作?

我有两个实体技能及其类型SkillType.这种关系看起来如下:

/**
 * @ORM\Entity
 * @ORM\Table(name="skills")
 */
class Skill
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var SkillType
     * @ORM\ManyToOne(targetEntity="SkillType", inversedBy="skills")
     * @ORM\JoinColumn(name="type_id", referencedColumnName="id")
     */
    protected $type;

    //Getters and Setters
}

/**
 * @ORM\Entity
 * @ORM\Table(name="skill_types")
 */
class SkillType
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var Skill[]|ArrayCollection
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Skill", mappedBy="type")
     */
    protected $skills;

    /**
     * SkillType constructor.
     */
    public function __construct()
    {
        $this->skills = new ArrayCollection();
    }

    //Getters and Setters
}

我还有一个形式创建这两个关系beetwen

class SkillType extends AbstractType
{
    /**
     * @inheritDoc
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('type', EntityType::class, [
                'multiple' => false,
                'class' => 'AppBundle\Entity\SkillType',
                'choice_label' => 'id',
                'by_reference' => false
            ]);
    }

    /**
     * @inheritDoc
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => 'AppBundle\Entity\Skill',
        ]);
    }
}

我是如何尝试修复错误的?

>添加,cascade = {“persist”}到两侧的映射
>添加$entityManager->合并($entity);在createForm之前(SkillType :: class,$entity

我的请求内容如下所示:{“skill”:{“type”:1},“id”:“1”}.因此,您可以看到它应该在具有id = 1的Skill和具有id = 1的SkillType之间创建关系.

我提交表单时收到的错误是:

必须管理传递到选择字段的实体.也许坚持他们在实体经理?

堆栈跟踪:

Symfony\Component\Form\Exception\RuntimeException: Entities passed to the choice field must be managed. Maybe persist them in the entity manager?
at n/a
    in /var/www/public_html/api-hb/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Form/ChoiceList/IdReader.php line 119

at Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader->getIdValue(object(SkillType))
    in /var/www/public_html/api-hb/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Form/ChoiceList/DoctrineChoiceLoader.php line 122

at Symfony\Bridge\Doctrine\Form\ChoiceList\DoctrineChoiceLoader->loadValuesForChoices(array(object(SkillType)), array(object(IdReader), 'getIdValue'))
    in /var/www/public_html/api-hb/vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/LazyChoiceList.php line 134

at Symfony\Component\Form\ChoiceList\LazyChoiceList->getValuesForChoices(array(object(SkillType)))
    in /var/www/public_html/api-hb/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoiceToValueTransformer.php line 37

at Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToValueTransformer->transform(object(SkillType))
    in /var/www/public_html/api-hb/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 1092

at Symfony\Component\Form\Form->normToView(object(SkillType))
    in /var/www/public_html/api-hb/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 352

at Symfony\Component\Form\Form->setData(object(SkillType))
    in /var/www/public_html/api-hb/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php line 57

at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapDataToForms(object(Skill), object(RecursiveIteratorIterator))
    in /var/www/public_html/api-hb/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 385

at Symfony\Component\Form\Form->setData(object(Skill))
    in /var/www/public_html/api-hb/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 477

at Symfony\Component\Form\Form->initialize()
    in /var/www/public_html/api-hb/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php line 226

at Symfony\Component\Form\FormBuilder->getForm()
    in /var/www/public_html/api-hb/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php line 39

最佳答案 我想这可能是db模式中的错误.首先检查你的数据库结构,如果我是正确的问题可能会发生在那里.我的概念是删除这种关系,运行doctrine schema update并再次创建这种关系.

我认为只在一个实体中出现此错误的原因没有在DB中正确反映,原因是之前的一些更改,即学说无法在架构更新中正确处理.

点赞