不能使用类型的对象作为数组

时间:2016-01-24 17:30:40

标签: php mysql symfony twig

我试图在页面验证中渲染我的订单,但在刷新我的validation.html.twig时出现此错误:

  

错误:无法使用FLY \ BookingsBundle \ Entity \ Address类型的对象作为   数组

     

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

但我的控制器中没有发现任何错误:

纸币

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

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));
}

1 个答案:

答案 0 :(得分:0)

您正在通过$order = $em->getRepository('FLYBookingsBundle:Address')->find($address['address']);将地址对象分配给$ order变量,然后您想在此处$order['tva'].....将其用作数组,并在代码中进一步使用。您必须使用$order->getTVA()等方法/属性处理$ order。