简体   繁体   English

无法将类型的对象用作数组

[英]Cannot use object of type as array

I'm trying to render my order in my page validation but when refresh my validation.html.twig i got this error: 我正在尝试在页面验证中呈现我的订单,但是刷新我的validation.html.twig时出现此错误:

Error: Cannot use object of type FLY\\BookingsBundle\\Entity\\Address as array 错误:无法将FLY \\ BookingsBundle \\ Entity \\ Address类型的对象用作数组

if (!isset($order['tva']['%'.$entity->getTva()->getValue()])) 如果(!isset($ order ['tva'] ['%'。$ entity-> getTva()-> getValue()]))

but i don't see anything wrong in my controller: 但我在控制器中看不到任何错误:

bill 法案

public function bill()
{
    $em = $this->getDoctrine()->getManager();
    $generator = $this->container->get('security.secure_random');
    $session = $this->getRequest()->getSession();
    $address = $session->get('address');
    $cart = $session->get('cart');
    $order = array();
    $totalHT = 0;
    $totalTTC = 0;

    $order = $em->getRepository('FLYBookingsBundle:Address')->find($address['address']);
    $entities = $em->getRepository('FLYBookingsBundle:Post')->findArray(array_keys($session->get('cart')));

    foreach($entities as $entity)
    {
        $priceHT = ($entity->getPrice() * $cart[$entity->getId()]);
        $priceTTC = ($entity->getPrice() * $cart[$entity->getId()] / $entity->getTva()->getMultiplicate());
        $totalHT += $priceHT;
        $totalTTC += $priceTTC;

        if (!isset($order['tva']['%'.$entity->getTva()->getValue()]))
            $order['tva']['%'.$entity->getTva()->getValue()] = round($priceTTC - $priceHT,2);
        else
            $order['tva']['%'.$entity->getTva()->getValue()] += round($priceTTC - $priceHT,2);

        $order['entity'][$order->getId()] = array('reference' => $order->getName(),
                                                 'quantity' => $cart[$entity->getId()],
                                                 'priceHT' => round($entity->getPrice(),2),
                                                 'priceTTC' => round($entity->getPrice() / $entity->getTva()->getMultiplicate(),2));
    }

    $order['address'] = array('surname' => $address->getSurname(),
        'name' => $address->getName(),
        'phone' => $address->getPhone(),
        'address' => $address->getAddress(),
        'zipcode' => $address->getZipcode(),
        'city' => $address->getCity(),
        'country' => $address->getCountry(),
        'complement' => $address->getComplement());

    $order['priceHT'] = round($totalHT,2);
    $order['priceTTC'] = round($totalTTC,2);
    $order['token'] = bin2hex($generator->nextBytes(20));

    return $order;
}

ValidationAction ValidationAction

public function validationAction()
{
    if ($this->get('request')->getMethod() == 'POST')
        $this->setAddressOnSession();
    $em = $this->getDoctrine()->getManager();
    $prepareOrder = $this->forward('FLYBookingsBundle:Post:prepareOrder');
    $order = $em->getRepository('FLYBookingsBundle:Address')->find($prepareOrder->getContent() );
    return $this->render('FLYBookingsBundle:Post:validation.html.twig', array('order' => $order));
}

You are assigning Address object to the $order variable by $order = $em->getRepository('FLYBookingsBundle:Address')->find($address['address']); 您正在通过$order = $em->getRepository('FLYBookingsBundle:Address')->find($address['address']);将Address对象分配给$ order变量$order = $em->getRepository('FLYBookingsBundle:Address')->find($address['address']); and then you want to use it as an array here $order['tva']..... and further down in your code. 然后要在$order['tva'].....中将其用作数组,然后在代码中进一步使用。 You have to work with $order by its methods/properties like $order->getTVA() . 您必须通过$ order的方法/属性来使用它,例如$order->getTVA()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM