我一直在Symfony2項目固定裝置上奮鬥了幾個小時。以下是錯誤我得到:Symfony2燈具與參考不加載(違反約束)
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`wpanel_dev`.`sshuser`, CONSTRAINT `FK_612DE3EE18F45C82` FOREIGN KEY (`website_id`) REFERENCES `Website` (`id`))
看了很多答案,我嘗試沒有成功這個方法:
php app/console doctrine:database:drop --force
php app/console doctrine:database:create
php app/console doctrine:schema:update --force
php app/console doctrine:fixtures:load (with or without --append)
這裏是產生錯誤的夾具代碼($新建 - >設置網站($ this-> getReference('website - '。$ i));)
<?php
namespace WPanel\AdminBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\FixtureInterface;
use WPanel\AdminBundle\Entity\SSHUser;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
class LoadSSHUserData extends AbstractFixture implements OrderedFixtureInterface
{
public function getOrder()
{
return 5;
}
public function load(ObjectManager $manager)
{
for($i = 1; $i <= 20; $i++) {
$new = new SSHUser();
$new->setUser('sshuser'.$i);
$new->setPassword('sshuser'.$i);
$new->setPath('/var/www/website'.$i.'.com');
$new->setWebsite($this->getReference('website-'.$i));
$manager->persist($new);
}
$manager->flush();
}
}
?>
我確定引用是好的。我一直在其他類上使用add/getReference,沒有任何問題。
感謝您的幫助!