dqwd71332 2016-11-12 10:32
浏览 29
已采纳

如何使用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: https://gyazo.com/bab9d948bdb7a2ae3375feb553ce07b2

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: https://gyazo.com/58582c628d8507e6b3eac48a7645f2a1

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}

  • 写回答

1条回答 默认 最新

  • douganbi7686 2016-11-13 09:47
    关注

    at first I would change your back link to am usually link, not an form.

    Your mistake in the condition is to check if price and the Produktas is equal. I think you forget the getPrice method here.

    But I would do this with a immutable object. Create a New money object, check this out at: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/embeddables.html

    I would suggest to create a Pocket entity and use the money entity in it.

    Now you can define the price in your car entity with the same money entity.

    In your car entity you should define a method called: isBuyable(Pocket $pocket). Here you should check that money value is greather than or equals as the money object inside of the car entity and return true if conditions matches.

    In your controller you dann use now The isBuyable method.

    Additionally you can create a new method in your car repository and fetch only cars that buyable, take a look at http://symfony.com/doc/current/doctrine/repository.html

    Hope it helps, Cheers, Robin

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。