在我使用symfony 1.4之后,我开始学习symfony2。我使用symfony 2.3的jobeet教程。我在第6天,我应该用夹具添加过期的工作。我完全搞砸了。我按照教程的每一个字母进行操作,但无法使其正常工作。运行php app/console doctrine:fixtures:load
时出现以下错误“
[PDOException] SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'Programming' for key 'UNIQ_64C19C15E237E06'
在评论中,我没有看到任何关于我得到的错误的评论。现在有2个工作。编程类别和设计类别。当我尝试添加另一个编程类别的作业时,它给了我一个错误。我知道查询的错误是什么,我只是不明白为什么要以这种方式进行查询。我发现问题是由编程类别的第二个工作引起的。我有LoadCategoryData.php:
class LoadCategoryData extends AbstractFixture implements OrderedFixtureInterface{
public function load(ObjectManager $em){
$design = new Category();
$design->setName('Design');
$programming = new Category();
$programming->setName('Programming');
$manager = new Category();
$manager->setName('Manager');
$administrator = new Category();
$administrator->setName('Administrator');
$em->persist($design);
$em->persist($programming);
$em->persist($manager);
$em->persist($administrator);
$this->addReference('category-design', $design);
$this->addReference('category-programming', $programming);
$this->addReference('category-manager', $manager);
$this->addReference('category-administrator', $administrator);
}
public function getOrder(){
return 1;
}
}
LoadJobData.php(我只会显示带编程类别的2):
//...
$job_expired->setCategory($em->merge($this->getReference('category-programming')));
//...
$job_sensio_labs->setCategory($em->merge($this->getReference('category-programming')));
//...
$em->persist($job_expired);
$em->persist($job_sensio_labs);
$em->flush();
我的Category.orm.yml看起来像这样:
Ibw\JobeetBundle\Entity\Category:
type: entity
table: category
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 255
unique: true
oneToMany:
jobs:
targetEntity: Job
mappedBy: category
manyToMany:
affiliates:
targetEntity: Affiliate
mappedBy: categories
我设置外键的Job.orm.yml:
manyToOne:
category:
targetEntity: Category
inversedBy: jobs
joinColumn:
name: category_id
referencedColumnName: id
我可以发现错误是由LoadJobData.php中的行引起的,我会设置类别,但我对symfony2来说太新了,无法找到解决方法。提前致谢,并为长篇文章感到抱歉。