Symfony2,Doctrine,Updation OneToMany条目

时间:2013-02-03 18:20:43

标签: symfony doctrine one-to-many

我有功能

private function updateCharacters($oApiKeyInfo) // argument is base entity
{
    $aXmlCharacter = $this->fXml->getXmlRowset($this->oXml, 'row');
    // insert new
    foreach ($aXmlCharacter as $oXmlCharacter)
    {
        $loopData = $this->fXml->getXmlAttributesAsArray($oXmlCharacter);

        $oApiKeyInfoCharacters = new apiKeyInfoCharacters();

        $oApiKeyInfoCharacters
                ->setKeyID($this->keyID)
                ->setCharacterID($loopData['characterID'])
                ->setCharacterName($loopData['characterName'])
                ->setCorporationID($loopData['corporationID'])
                ->setCorporationName($loopData['corporationName'])
                ->set_apiKeyInfo_byKeyID($oApiKeyInfo);


        $this->em->persist($oApiKeyInfoCharacters);
    }

// $this->em->flush() is in main (public) function
}

但是,它会创建dublicates ...而且我想在db中只有$ aXmlCharacter(数组)中的条目,其他必须删除

现在上面的代码只是添加新条目,但我需要删除以前的

有人可以帮忙处理吗?并请出示工作实例

1 个答案:

答案 0 :(得分:1)

Dublicate条目我看不到uniquenes的任何定义 正如文档所示,删除Object vom数据库很简单。

$product = $em->getRepository('YourBundle::apiKeyInfoCharacters')->find($id);
$em->remove($product);
$em->flush();

但为什么要删除现有的而不是更新?