Reputation: 49
I am a beginner in symfony.
I installed fixtures Bundle .I want to add products in my table produced which is linked with two other tables media and categories. Using the reference and spanking an update, I can see the result of addition in the category table and media but for the Products table I get an error . Here the code of the class produits.php
/**
* @ORM\ManyToOne(targetEntity="Ecommerce\EcommerceBundle\Entity\Categories",cascade={"persist","remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $categorie;
here the code of the class produitsData.php
namespace Ecommerce\EcommerceBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Ecommerce\EcommerceBundle\Entity\Produits;
class ProduitsData extends AbstractFixture implements OrderedFixtureInterface
{
public function load(ObjectManager $manager)
{
$produit1 = new Produits();
$produit1->setCategorie($this->getReference('categorie1'));
$produit1->setDescription("Le poivron rouge est un groupe de cultivars de l'espèce Capsicum annuum.");
$produit1->setDisponible('1');
$produit1->setImage($this->getReference('media3'));
$produit1->setNom('Poivron rouge');
$produit1->setPrix('1.99');
$produit1->setTva($this->getReference('tva2'));
$manager->persist($produit1);
$produit2 = new Produits();
$produit2->setCategorie($this->getReference('categorie1'));
$produit2->setDescription("Piment est généralement associé à la saveur du piquant (pimenté).");
$produit2->setDisponible('1');
$produit2->setImage($this->getReference('media4'));
$produit2->setNom('Piment');
$produit2->setPrix('3.99');
$produit2->setTva($this->getReference('tva2'));
$manager->persist($produit2);
$produit3 = new Produits();
$produit3->setCategorie($this->getReference('categorie1'));
$produit3->setDescription("La tomate est une espèce de plantes herbacées de la famille des Solanacées, originaire du nord-ouest de l'Amérique du Sud.");
$produit3->setDisponible('1');
$produit3->setImage($this->getReference('media5'));
$produit3->setNom('Tomate');
$produit3->setPrix('0.99');
$produit3->setTva($this->getReference('tva2'));
$manager->persist($produit3);
$produit4 = new Produits();
$produit4->setCategorie($this->getReference('categorie1'));
$produit4->setDescription("Le poivron vert est un groupe de cultivars de l'espèce Capsicum annuum.");
$produit4->setDisponible('1');
$produit4->setImage($this->getReference('media6'));
$produit4->setNom('Poivron vert');
$produit4->setPrix('2.99');
$produit4->setTva($this->getReference('tva2'));
$manager->persist($produit4);
$produit5 = new Produits();
$produit5->setCategorie($this->getReference('categorie2'));
$produit5->setDescription("Le raisin est le fruit de la Vigne. Le raisin de la vigne cultivée Vitis vinifera est un des fruits les plus cultivés au monde, avec 68 millions de tonnes produites en 2010.");
$produit5->setDisponible('1');
$produit5->setImage($this->getReference('media7'));
$produit5->setNom('Raisin');
$produit5->setPrix('0.97');
$produit5->setTva($this->getReference('tva2'));
$manager->persist($produit5);
$produit6 = new Produits();
$produit6->setCategorie($this->getReference('categorie2'));
$produit6->setDescription("L’orange est un agrume, fruit des orangers, des arbres de différentes espèces de la famille des Rutacées ou d'hybrides de ceux-ci.");
$produit6->setDisponible('1');
$produit6->setImage($this->getReference('media8'));
$produit6->setNom('Orange');
$produit6->setPrix('1.20');
$produit6->setTva($this->getReference('tva2'));
$manager->persist($produit6);
$manager->flush();
}
public function getOrder()
{
return 4;
}
}
here the exception :
C:\wamp\www\ecommerce>php app/console doctrine:fixtures:load
Careful, database will be purged. Do you want to continue y/N ?y
> purging database
> loading [1] Ecommerce\EcommerceBundle\DataFixtures\ORM\MediaData
> loading [2] Ecommerce\EcommerceBundle\DataFixtures\ORM\CategoriesData
> loading [3] Ecommerce\EcommerceBundle\DataFixtures\ORM\TvaData
> loading [4] Ecommerce\EcommerceBundle\DataFixtures\ORM\ProduitsData
[Doctrine\DBAL\DBALException]
An exception occurred while executing 'INSERT INTO produits (nom, description, prix, disponible, image_id, categorie_id, tva_id) VALUES (?, ?, ?, ?, ?
, ?, ?)' with params ["Piment", "Piment est g\u00e9n\u00e9ralement associ\u
00e9 \u00e0 la saveur du piquant (piment\u00e9).", "3.99", "1", 92, 23, 24]
:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '23'
for key 'UNIQ_BE2DDF8CBCF5E72D'
[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '23'
for key 'UNIQ_BE2DDF8CBCF5E72D'
doctrine:fixtures:load [--fixtures [FIXTURES]] [--append] [--em EM] [--shard SHA
RD] [--purge-with-truncate] [--multiple-transactions]
Please Helps :( :(
Upvotes: 0
Views: 2900
Reputation: 378
It looks like the categorie_id attribute has an unique key. Make sure that in the Produits entity file,
Ecommerce\EcommerceBundle\Entity\Produits
the @ORM\Column for categorie is set to unique = false
. After that you need to update the changes in the schema,
php app/console doctrine:schema:update --force
and then try running fixtures again. If it continues giving integrity constraint errors. Check for other attributes that might have unique keys. If all else fails, try deleting the database and creating again from a new,
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
Upvotes: 0