【问题标题】:Why the values are converted to an Array?为什么将值转换为数组?
【发布时间】:2014-09-15 00:38:14
【问题描述】:

我有这个代码:

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 行 276

谁能告诉我在哪里查找或查找此错误?

运行一些测试

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

【问题讨论】:

  • 参数必须是Person类的实例。
  • @Ohgodwhy 我添加了一些额外的信息,你能指出我正确的方向吗?我迷路了,不知道该往哪里看
  • Please observe the this link。我相信这将有助于让您更加清楚。
  • @Ohgodwhy 这正是我正在做的,首先创建$entityPerson,然后将其设置为$entityOrder,那里有什么问题?在另一边$person_type2 作为值,但我尝试在条件中使用die("Enter") 并且它不起作用,出现相同的错误

标签: php symfony symfony-2.5


【解决方案1】:

好吧,经过多次测试终于发现问题所在,我完全忘记了LegalPersonType.php表单中的这个功能:

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

这是导致问题的原因,感谢您的帮助和时间

【讨论】:

    猜你喜欢
    • 2017-03-28
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    相关资源
    最近更新 更多