简体   繁体   中英

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:

Error: Cannot use object of type FLY\\BookingsBundle\\Entity\\Address as array

if (!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

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']); and then you want to use it as an array here $order['tva']..... and further down in your code. You have to work with $order by its methods/properties like $order->getTVA() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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