【问题标题】:Why my relation does not want to work?为什么我的关系不想工作?
【发布时间】:2016-09-13 10:04:59
【问题描述】:

我有两个实体Skill 及其类型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
}

我还有一个表单来创建这两者之间的关系

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',
        ]);
    }
}

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

  1. , cascade={"persist"} 添加到两边的映射中
  2. createForm(SkillType::class, $entity 之前添加$entityManager->merge($entity);

我的请求内容如下:{"skill":{"type":1},"id":"1"}。如您所见,它应该在Skillid=1SkillTypeid=1 之间建立关系。

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

必须管理传递给选择字段的实体。也许将它们保留在实体管理器中?

堆栈跟踪:

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

【问题讨论】:

    标签: php doctrine symfony


    【解决方案1】:

    我想这可能是 db 架构中的错误。首先检查您的数据库结构,如果我是对的,那里可能会出现问题。我的概念是删除这个关系,运行学说模式更新并再次创建这个关系。

    我认为您仅在一个实体中收到此错误的原因并没有正确反映在数据库中,因为以前的一些更改在架构更新时无法正确处理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      • 2019-11-14
      • 2012-12-07
      相关资源
      最近更新 更多