为什么将值转换为数组?

时间:2014-09-15 00:38:14

标签: php symfony symfony-2.5

我有这段代码:

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行

这导致我的申请出现此问题:

  

捕获致命错误:参数1传递给   Tanane \ FrontendBundle \ Entity \ Orders :: setPerson()必须是一个实例   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 line   276

任何人都可以告诉我在哪里寻找或发现此错误吗?

运行一些测试

运行一些测试后(填写表格并按照普通用户的意愿发送数据)我很困惑,不知道还有什么办法可以解决这个问题。该应用程序有两种类型的表单来处理OrdersNaturalLegal。我测试了第一个Natural并且一切都很好,表单验证并且流程完全没有问题。现在,如果我通过第二种形式出现上述错误,为什么?是否完全相同的过程和值是正常的,因为$person_type取2并且它是integer所以,任何建议?我现在变得疯狂

1 个答案:

答案 0 :(得分:0)

经过多次测试后终于发现了什么问题,我完全忘记了LegalPersonType.php形式的这个功能:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Tanane\FrontendBundle\Entity\LegalPerson'
    ));
}

这导致了这个问题,谢谢你的帮助和时间