我有这个代码:
public function saveAction(Request $request)
{
$orders = $request->get('orders');
$sameAddress = $request->get('same_address');
// NaturalPerson: 1 | LegalPerson: 2
$person_type = isset($orders['person']['nat']) ? 1 : 2;
$register_type = isset($orders['person']['nat']) ? array("natural") : array("legal");
$entityOrder = new Orders();
$formOrder = $this->createForm(new OrdersType($register_type), $entityOrder);
$formOrder->handleRequest($request);
$em = $this->getDoctrine()->getManager();
$em->getConnection()->beginTransaction();
$errors = "";
$is_new = false;
if ($formOrder->isValid())
{
if ($person_type === 1)
{
// Set NaturalPerson entity
$entityPerson = $em->getRepository('FrontendBundle:NaturalPerson')->findOneBy(array("ci" => $orders['person']['ci']));
if (!$entityPerson)
{
$entityPerson = new NaturalPerson();
$entityPerson->setPersonType($person_type);
$entityPerson->setDescription($orders['person']['nat']['description']);
$entityPerson->setContactPerson($orders['person']['nat']['contact_person']);
$entityPerson->setIdentificationType($orders['person']['identification_type']);
$entityPerson->setCI($orders['person']['ci']);
$is_new = true;
}
}
elseif ($person_type === 2)
{
// Set LegalPerson entity
$entityPerson = $em->getRepository('FrontendBundle:LegalPerson')->findOneBy(array("rif" => $orders['person']['rif']));
if (!$entityPerson)
{
$entityPerson = new LegalPerson();
$entityPerson->setPersonType($person_type);
$entityPerson->setDescription($orders['person']['leg']['description']);
$entityPerson->setContactPerson($orders['person']['leg']['contact_person']);
$entityPerson->setIdentificationType($orders['person']['identification_type']);
$entityPerson->setRIF($orders['person']['rif']);
$is_new = true;
}
}
....
$entityOrder->setPerson($entityPerson);
$em->persist($entityOrder);
$em->flush();
}
else
{
$this->get('ladybug')->log($this->getFormErrors($formOrder));
$message = 'ERROR AL PROCESAR LOS DATOS';
$em->getConnection()->rollback();
}
return $this->render('FrontendBundle:Site:process.html.twig', array('message' => $message, 'errors' => $errors));
}
由于某种原因,在某个地方,我找不到,实体正在到达一个数组,如stacktrace所示:
at Orders ->setPerson (array('rif' => '5345345345', 'identification_type' => 'V', 'description' => 'uiyiyuiyuiy',
'contact_person'=>'ertertet')在/var/www/html/tanane/vender/symfony/symfony/src/symfony/Component/PropertyAccess/PropertyAccessor.php中在第438行
导致我的应用程序出现此问题的原因:
可捕获的致命错误:参数1传递给Tanane\FrontendBundle\Entity\Orders::setPerson()必须是一个实例的Tanane\FrontendBundle\Entity\Person,给定数组,在中调用/var/www/html/tanane/vender/symfony/symfony/src/symfony/Component/PropertyAccess/PropertyAccessor.php第438行,定义于/var/www/html/tanane/src/tanane/FrontendBundle/Entity/Orders.php行276
有人能告诉我在哪里查找这个错误吗?
运行一些测试
在运行了一些测试(像任何普通用户一样填写表格并发送数据)后,我很困惑,不知道还能做些什么来解决这个问题。应用程序有两种类型的表单来处理Orders
:Natural
和Legal
。我测试了第一个Natural
,一切都很好,表单验证了,流程完全没有问题。现在,如果我通过第二种形式,上面描述的错误出现了,为什么?由于$person_type
取2,是integer
,所以相同的过程和值是否正确?有什么建议吗?我在这一点上疯了
经过几次测试,终于发现了问题所在,我完全忘记了LegalPersonType.php
表单中的这个函数:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'TananeFrontendBundleEntityLegalPerson'
));
}
这就是问题的原因,感谢您的帮助和时间