我这样做是有效的。
//存储库
public function InsertListaccts($Table = '<listaccts>', $Data)
{
$this->getEntityManager()->getConnection()->insert($Table, $Data);
}
// controller
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值我得到然后重复值。 目前我有UserName的唯一值,现在如果数据库中已存在相同的UserName,我想更新它。 是否可以对此查询执行操作? $ this-&gt; getEntityManager() - &gt; getConnection() - &gt; 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 :(得分: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建议的那样#34;但使用param绑定更安全 - sakhunzai&#34;如果有人按照他的意思转移这个查询,那将非常有帮助。