Reputation: 6556
I have two entities Skill
and its type SkillType
. The relationship looks as follow:
/**
* @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
}
I have also a form creating the relationship beetwen this two
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',
]);
}
}
How i have tried to fix the error?
, cascade={"persist"}
to mapping on either side also on both sides$entityManager->merge($entity);
before createForm(SkillType::class, $entity
My request content looks as follow: {"skill":{"type":1},"id":"1"}
. So as you can see it should create a relation between Skill
with id=1
and SkillType
with id=1
.
The error i get when i submit form is:
Entities passed to the choice field must be managed. Maybe persist them in the entity manager?
Stack Trace:
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
Upvotes: 4
Views: 115
Reputation: 466
I suppose this could be error in db schema. First check your db structure, if i'm right problem could occur there. My concept is to delete this relation, run doctrine schema update and create this relation again.
I think reason that you get this error only in one Entity is not properly reflected in DB due some previous changes that doctrine could not handle properly on schema update.
Upvotes: 2