检查Doctrine2中的两列重复 [英] Checking two columns duplicate in Doctrine2
本文介绍了检查Doctrine2中的两列重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了一个API调用,用于提取MainTable中的.json文件.
I wrote an API call that extracts .json file in MainTable.
在LangTable中,我有3种语言,并且我正在提取每种语言的json数据.
In LangTable I have 3 languages and I am extracting the json data for every language.
Json数据具有要填充的键和区域字段.
Json data has key and locale field to populate.
要转义重复项,应该没有重复的键值<->语言环境值,因此应检查每个键值.
To escape duplicate there should be no duplicated key<->locale values, so that should be checked for each key.
在持久存储数据库之前如何检查?
How to check that before persisting the database?
下面的代码有效.现在,我需要添加我提到的逻辑.
Code below works. Now I need to add logic I mentioned.
有人可以帮忙吗?
$file = file_get_contents('translation.json');
$jsonData = json_decode($file, true);
$findLanguage = $this->getLangTableRepository()->findAll();
foreach ( $findLanguage as $locale) {
foreach ($jsonData as $data) {
$newTranslation = new MainTable();
$newTranslation->setKey($data);
$newTranslation->setLocale($locale->getLocale());
$this->em->persist($newTranslation);
}
}
$this->em->flush();
dump('done!');die;
推荐答案
您可以使用
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
UniqueEntity像这样添加关于唯一性的约束:
UniqueEntity to add a constraint about being unique like so :
/**
* @ORM\Table(name="entity")
* @ORM\Entity(repositoryClass="App\MainBundle\Entity\EntityRepository")
* @UniqueEntity(fields={"attributeA"}, message="Attribute A already exists
*/
这篇关于检查Doctrine2中的两列重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文