Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony+原则-仅当一系列foreach结束时才截断表_Php_Symfony_Oop_Doctrine - Fatal编程技术网

Php Symfony+原则-仅当一系列foreach结束时才截断表

Php Symfony+原则-仅当一系列foreach结束时才截断表,php,symfony,oop,doctrine,Php,Symfony,Oop,Doctrine,我的数据库中有5个实体: Customer Location Machine_1 Machine_2 Machine_3 客户与地点存在一对一关系一个客户可以有多个地点,机器1、机器2和机器3与地点存在多对一关系一个地点可以有多台机器 我已经在数据库中存储了实体的所有数据。 我有一个API服务站,可以更新三台机器的数据。 我就是这样做的: /** * @Route("/saveData") * @Method("POST") **/ public fun

我的数据库中有5个实体:

Customer
Location
Machine_1
Machine_2
Machine_3
客户与地点存在一对一关系一个客户可以有多个地点,机器1、机器2和机器3与地点存在多对一关系一个地点可以有多台机器

我已经在数据库中存储了实体的所有数据。 我有一个API服务站,可以更新三台机器的数据。 我就是这样做的:

    /**
    * @Route("/saveData")
    * @Method("POST")
    **/
    public function saveDataAction (Request $request) {
      $data = $request->getContent();
      $dataDecode = json_decode($data);

      if (is_object($dataDecode)) {
        foreach ($dataDecode->customer as $customer) {
          //get data
          foreach ($dataDecode->location as $location) {
            //getdata
            foreach ($dataDecode->machine_1 as $machine_1) {
              //get and set data
            }
            foreach ($dataDecode->machine_2 as $machine_2) {
              //get and set data
            }
            foreach ($dataDecode->machine_3 as $machine_3) {
              //get and set data
            }
          }
        }
在插入新数据之前,我需要截断所有的机械表,但只有在foreach循环内部一切正常的情况下,才会发生这种情况

类似这样,但只有在代码的其余部分执行良好时:

if (is_object($dataDecode)) {
   $em->createQuery('DELETE FROM AppBundle:Machine_1')->execute();
   $em->createQuery('DELETE FROM AppBundle:Machine_2')->execute();
   $em->createQuery('DELETE FROM AppBundle:Machine_3')->execute();
   .....
}
我该怎么做?谢谢

您可以使用PHP的异常机制。 如果代码的其余部分执行良好,我不知道您的确切意思,但您可以使用以下内容:

/**
* @Route("/saveData")
* @Method("POST")
**/
public function saveDataAction (Request $request) {
  $data = $request->getContent();
  $dataDecode = json_decode($data);

  if (is_object($dataDecode)) {
    try{
      foreach ($dataDecode->customer as $customer) {
        //get data
        if ($something_is_wrong){
           throw new \Exception('message');
        }
        foreach ($dataDecode->location as $location) {
          //getdata
          if ($something_is_wrong){
             throw new \Exception('message');
          }
          foreach ($dataDecode->machine_1 as $machine_1) {
            //get and set data
            if ($something_is_wrong){
             throw new \Exception('message');
            }
          }
          foreach ($dataDecode->machine_2 as $machine_2) {
            //get and set data
            if ($something_is_wrong){
              throw new \Exception('message');
            }
          }
          foreach ($dataDecode->machine_3 as $machine_3) {
            //get and set data
            if ($something_is_wrong){
               throw new \Exception('message');
            }
          }
        }
        //These lines of code will only execute if no Exception is thrown:
        $em->createQuery('DELETE FROM AppBundle:Machine_1')->execute();
        $em->createQuery('DELETE FROM AppBundle:Machine_2')->execute();
        $em->createQuery('DELETE FROM AppBundle:Machine_3')->execute();
      } catch (\Exception $e){
         //These lines will only execute if something went wrong
      }
    }

你说这首歌结局好是什么意思?你尝试了什么,为什么没有成功?@yivi我的意思是一切都很顺利,我没有犯任何错误。在我知道检索和设置数据的过程结束之前,我不想截断表。您尝试了什么?我尝试在控制器开始时截断所有内容,但这不是最佳解决方案。因此,您尝试了与您要求的不同的操作。我的意思是:你试图实现这个特定的结果是什么?我收到的数据是一个JSON文件。它可以有多个位置,然后有多台机器。例如,如果我收到4个客户,则可能会多次执行foreach。所以我需要在foreach on customer完成并且一切顺利时截断。好吧,我想你可以根据自己的需要调整这个想法。你能给我一个提示吗?在插入来自API服务的新数据之前,我只需截断表一次。这与您要求的不同,不是吗?你说过只有在foreach块内部没有错误的情况下才能删除所有元素……是的,但我解释了一般情况。foreach on customer就在那里,因为我可以有多个客户、多个地点和多台机器要保存。对不起,有什么困惑