samedi 12 novembre 2016

How to compare objects in controller using IF symfony2 PHP

I am a beginner at symfony programming and I am curious how can I compare two objects in controller. This is how my page looks like: http://ift.tt/2f2iYT2

At the top left there is my money amount and I want to buy a car which has its own price. Amount of money has its own place in a database and car have separate one. By pressing Buy Car it opens a form which look like this: http://ift.tt/2eqTRgq

There is two options: Back which redirects to front page and Delete(Buy) which deletes car from database. This is how function in controller looks like:

public function deleteAction(Request $request, Car $car){
$form = $this -> createFormBuilder($car)
        ->add('save',SubmitType::class,['label' => 'DELETE'])
        ->getForm();

    $form2 = $this -> createFormBuilder($car)
        ->add('save2',SubmitType::class,['label' => 'BACK'])
        ->getForm();


    $form->handleRequest($request);
    $form2->handleRequest($request);
   if ($form->isSubmitted() && $form->isValid()) {


        $em = $this ->getDoctrine()->getManager();
        $baze = $em->getRepository('AppBundle:Produktas')->find(1);
       $price =$car->getPrice();



      if($price < $baze) {
        $baze->setKaina($baze->getKaina() - $price);
           $em->remove($car);
     }

        $em->flush();
        return $this->redirectToRoute('car_index');
    }

The question is: How can I compare two objects in if in a appropriate way? I want to make if(my money>Car Price){I CAN BUY IT}

Aucun commentaire:

Enregistrer un commentaire