我已阅读有关此错误的每个帖子,但我无法弄清楚这是来自何处。
我有一个表单,其中包含许多从数组中填充的复选框字段。但是在我的实体中,我将所有字段转换为逗号分隔的字符串(是的,我知道这很难看)并且所讨论的字段都可以正常工作,但是我收到此错误并且我无法形象问题来自哪里。当我调试这些字段时,它给了我有问题的数组,我能够修复它们
我还有另外4组EntityType选项列表,从4个其他数据库表中填充。所有数据都适用于所有实体,但是当我尝试持久保存数据库时,我得到了这个错误,但是我无法找到任何对实体或数组/字段的引用这指的是。我在下面看到的只是空数组。
如果看到我的实体和FormType有助于我发布这些内容。
[1] Symfony\Component\Debug\Exception\ContextErrorException: Notice: Array to string conversion
at n/a
in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php line 73
at Symfony\Component\Debug\ErrorHandler->handleError('8', 'Array to string conversion', '/Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php', '73', array('choice' => array('')))
in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php line 73
at Symfony\Component\Form\ChoiceList\ArrayChoiceList->Symfony\Component\Form\ChoiceList\{closure}(array(''))
in line
at call_user_func(object(Closure), array(''))
in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php line 158
at Symfony\Component\Form\ChoiceList\ArrayChoiceList->getValuesForChoices(array(array('')))
in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoiceToValueTransformer.php line 37
at Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToValueTransformer->transform(array(''))
in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 1092
at Symfony\Component\Form\Form->normToView(array(''))
in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 352
at Symfony\Component\Form\Form->setData(array(''))
in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php line 57
at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapDataToForms(object(Programs), object(RecursiveIteratorIterator))
in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 385
at Symfony\Component\Form\Form->setData(object(Programs))
in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 477
at Symfony\Component\Form\Form->initialize()
in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php line 226
at Symfony\Component\Form\FormBuilder->getForm()
in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php line 39
at Symfony\Component\Form\FormFactory->create('AppBundle\Form\ProgramsType', object(Programs), array())
in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php line 309
at Symfony\Bundle\FrameworkBundle\Controller\Controller->createForm('AppBundle\Form\ProgramsType', object(Programs))
in /Applications/MAMP/htdocs/mpdb/src/AppBundle/Controller/DefaultController.php line 37
at AppBundle\Controller\DefaultController->nominateAction(object(Request))
in line
at call_user_func_array(array(object(DefaultController), 'nominateAction'), array(object(Request)))
in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 148
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 66
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
in /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php line 169
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
INFO - Matched route "{route}".
INFO - Populated the TokenStorage with an anonymous Token.
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - SELECT k0_.title AS title_0, k0_.slug AS slug_1, k0_.id AS id_2 FROM keywords k0_
DEBUG - SELECT c0_.title AS title_0, c0_.slug AS slug_1, c0_.is_effstrat AS is_effstrat_2, c0_.id AS id_3 FROM categories c0_ WHERE c0_.is_effstrat = 'YES'
DEBUG - SELECT r0_.title AS title_0, r0_.category AS category_1, r0_.report_text AS report_text_2, r0_.id AS id_3 FROM risk_factors r0_
DEBUG - SELECT p0_.title AS title_0, p0_.category AS category_1, p0_.report_text AS report_text_2, p0_.id AS id_3 FROM protective_factors p0_
CRITICAL - Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: "Notice: Array to string conversion" at /Applications/MAMP/htdocs/mpdb/vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php line 73
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
DEBUG - Notified event "{event}" to listener "{listener}".
我的数据库结构有5个主表和每个多个表的连接表
programs (main table)
- id
- other columns
keywords
- id
- other columns
programs_keywords
- program_id
- keyword_id
等表格类别,protective_factors,risk_factors
计划实体
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Categories", inversedBy="program")
* @ORM\JoinTable(name="programs_categories",
* joinColumns={
* @ORM\JoinColumn(name="program_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
* }
* )
* @Assert\Count(min = 1, minMessage = "You must select at least one option.")
*/
private $category;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Keywords", inversedBy="program")
* @ORM\JoinTable(name="programs_keywords",
* joinColumns={
* @ORM\JoinColumn(name="program_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="keyword_id", referencedColumnName="id")
* }
* )
* @Assert\Count(min = 1, minMessage = "You must select at least one option.")
*/
private $keyword;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="ProtectiveFactors", inversedBy="program")
* @ORM\JoinTable(name="programs_protective_factors",
* joinColumns={
* @ORM\JoinColumn(name="program_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="protective_factor_id", referencedColumnName="id")
* }
* )
* @Assert\Count(min = 1, minMessage = "You must select at least one option.")
*/
private $protectiveFactor;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="RiskFactors", inversedBy="program")
* @ORM\JoinTable(name="programs_risk_factors",
* joinColumns={
* @ORM\JoinColumn(name="program_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="risk_factor_id", referencedColumnName="id")
* }
* )
* @Assert\Count(min = 1, minMessage = "You must select at least one option.")
*/
private $riskFactor;
ProgramsType表单类 这已经添加了我的实体,但是如上所述,还有很多其他的ChoiceType字段从数组中填充,但这些都可以正常工作。
->add('keyword', EntityType::class, array( 'class' => 'AppBundle:Keywords', 'choice_label' => 'title', 'multiple' => true, 'expanded' => true ))
->add('riskFactor', EntityType::class, array( 'class' => 'AppBundle:RiskFactors', 'choice_label' => 'title', 'multiple' => true, 'expanded' => true ))
->add('protectiveFactor', EntityType::class, array( 'class' => 'AppBundle:ProtectiveFactors', 'choice_label' => 'title', 'multiple' => true, 'expanded' => true ))
->add('category', EntityType::class, array( 'class' => 'AppBundle:Categories', 'query_builder' => function (EntityRepository $er) {
$qb = $er->createQueryBuilder('c');
$qb->where($qb->expr()->eq('c.isEffstrat', $qb->expr()->literal('YES')));
return $qb;
},'choice_label' => 'title', 'multiple' => true, 'expanded' => true ))
答案 0 :(得分:1)
终于解决了这个问题。问题是我将未设置为multiple = true的ChoiceType字段转换为数组并返回到我的实体中。
简单的错误却忽略了很多次。