2017-04-03 48 views
0

Iam这样做,它正在工作。在重复键上插入API结果数据库更新

//库

public function InsertListaccts($Table = '<listaccts>', $Data) 
    { 
     $this->getEntityManager()->getConnection()->insert($Table, $Data); 
     } 

//控制器

public function ApiAction() 
     { 
      $Cpanel = new \Gufy\CpanelPhp\Cpanel; 
      $Cpanel->setAuthType('hash'); 
      $Cpanel->setHost($this->container->getParameter('api_host')); 
      $Cpanel->setAuthorization($this->container->getParameter('api_user'), $this->container->getParameter('api_hash')); 
      $Cpanel->setTimeout(50); 

      $Arguments = array(); 
      $QueryCpanel= $Cpanel->__call('listaccts', $Arguments); 
      $em = $this->getDoctrine()->getEntityManager(); 
      $json = json_decode($QueryCpanel, true); 

      foreach ($json ['acct'] as $List) { 
      $Data['Domain'] =$List ['domain']; 
      $Data['IP'] = $List ['ip']; 
      $Data['UserName'] = $List ['user']; 
      $Data['Email'] = $List ['email']; 
      $Data['StartDate'] = $List ['startdate']; 
      $Data['DiskPartition'] = $List ['partition']; 
      $Data['Quota'] = $List ['disklimit']; 
      $Data['DiskSpaceUsed'] = $List ['diskused']; 
      $Data['Package'] = $List ['plan']; 
      $Data['Theme'] = $List ['theme']; 
      $Data['Owner'] = $List ['owner']; 
      $Data['UnixStartDate'] = $List ['unix_startdate']; 

       $em->getRepository('AppBundle:Listaccts') 
        ->InsertListaccts('listaccts', $Data); 
     } 

荫试图从API到数据库中插入数据,但问题是,如果evrytime我运行查询它保持插入所有API值I获取重复值。 目前我对UserName有唯一的值,现在如果数据库中已经存在相同的UserName,我想更新它。 有没有可能在这个查询上做? $ this-> getEntityManager() - > getConnection() - > insert($ Table,$ Data);

我试图做这样的事情,插入部分工作。我怎样才能让这个查询工作,或是否有更好的方式来做到这一点

public function InsertListaccts($Table = '<listaccts>', $Data) 
    { 
     // $this->getEntityManager()->getConnection()->insert($Table, $Data); 
     $Sql="INSERT INTO $Table(`ListacctsID`,`Domain`, `IP`, `UserName`, 
           `Email`, `StartDate`, `DiskPartition`, `Quota`, `DiskSpaceUsed`, `Package`, 
           `Theme`, `Owner`, `UnixStartdate`) VALUES 
           ('{$Data['Domain']}', '{$Data['IP']}', '{$Data['UserName']}', '{$Data['Email']}', '{$Data['StartDate']}', '{$Data['DiskPartition']}', '{$Data['Quota']}', '{$Data['DiskSpaceUsed']}', 
           '{$Data['Package']}', '{$Data['Theme']}', '{$Data['Owner']}', '{$Data['UnixStartDate']}') 
         ON DUPLICATE KEY UPDATE DOMAIN =VALUES(DOMAIN),IP=VALUES(IP),UserName=VALUES(UserName),Email=VALUES(Email),StartDate=VALUES(StartDate),DiskPartition=VALUES(DiskPartition),Quota=VALUES(Quota) 
           ,DiskSpaceUsed=VALUES(DiskSpaceUsed),Package=VALUES(Package),Theme=VALUES(Theme),Owner=VALUES(Owner),UnixStartdate=VALUES(UnixStartdate) 
           "; 
     $Stm = $this->getEntityManager()->getConnection()->prepare($Sql); 
     $Stm->execute(); 
     // return $Data; 

     } 

我不是真的想改变任何控制器。

+0

您需要一个唯一的或主要的关键字,用于ON DUPLICATE KEY UPDATE的工作。 //如果您只想避免刷新结果页面再次发送(并因此插入)数据的问题,则实现POST/Redirect/GET模式。 – CBroe

+0

我的表中有主键。但我没有任何API的独特价值。我想让查询工作。 – user2268276

+0

然后,您不能使用ON DUPLICATE KEY UPDATE ... – CBroe

回答

0
public function InsertListaccts($Table = '<listaccts>', $Data) 
    { 
     // $this->getEntityManager()->getConnection()->insert($Table, $Data); 
     $Sql="INSERT INTO $Table(`Domain`, `IP`, `UserName`, 
           `Email`, `StartDate`, `DiskPartition`, `Quota`, `DiskSpaceUsed`, `Package`, 
           `Theme`, `Owner`, `UnixStartdate`) VALUES 
           ('{$Data['Domain']}', '{$Data['IP']}', '{$Data['UserName']}', '{$Data['Email']}', '{$Data['StartDate']}', '{$Data['DiskPartition']}', '{$Data['Quota']}', '{$Data['DiskSpaceUsed']}', 
            '{$Data['Package']}', '{$Data['Theme']}', '{$Data['Owner']}', '{$Data['UnixStartDate']}') 
            ON DUPLICATE KEY UPDATE 
            Domain= '{$Data['Domain']}', IP ='{$Data['IP']}', 
            UserName='{$Data['UserName']}', Email='{$Data['Email']}', 
            StartDate='{$Data['StartDate']}', 
            DiskPartition='{$Data['DiskPartition']}', Quota='{$Data['Quota']}', 
            DiskSpaceUsed='{$Data['DiskSpaceUsed']}', 
            Package='{$Data['Package']}', Theme= '{$Data['Theme']}', 
            Owner='{$Data['Owner']}', UnixStartdate='{$Data['UnixStartDate']}' 
            "; 
     $Stm = $this->getEntityManager()->getConnection()->prepare($Sql); 
     $Stm->execute(); 

     } 

此查询工作正常,但由于sakhunzai建议“但使用PARAM结合是更安全 - sakhunzai”如果有人在他是什么意思这将是非常有益的转让此查询。

+0

@sakhunzai我folowed你的answere它给了我这个错误 – user2268276

+0

错误我有)在最后不应该在那里,Thanx @sakhunzai我想我现在拥有它。 – user2268276

相关问题