我有这个代码:
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));
}
由于某种原因,在某处,我找不到,实体正在到达一个数组,作为堆栈跟踪显示在这一行:
at Orders ->setPerson (array('rif' => '5345345345', 'identification_type' => 'V', 'description' => 'uiyiyuiyuiy',
'contact_person' => 'ertertet')) 在 /var/www/html/tanane/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php 第 438 行
这导致我的应用程序出现此问题:
可捕获的致命错误:传递给 Tanane\FrontendBundle\Entity\Orders::setPerson() 的参数 1 必须是 Tanane\FrontendBundle\Entity\Person 的实例,给定数组,在 /var/www/html/tanane/vendor/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
这样,有什么建议吗?在这一点上我快疯了