Créer des fixtures en base entre 2 entités liées en ManyToOne

90608
,

Bonjour,

Voila je rencontre un petit problème avec mon code. Merci de votre aide !

Ce que je fais

Créer des données avec des fixtures. J'ai 2 entités : Facture et User. Un user peut avoir plusieurs factures.
Dans Facture.php j'ai rajouté cette propriété:

/**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="factures", cascade="persist")
     * @ORM\JoinColumn(nullable=false)
     */
    private $user;

Voici FactureFixtures.php:

<?php

namespace App\DataFixtures;


use App\Entity\Facture;
use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;

class FactureFixtures extends Fixture
{
    public function load(ObjectManager $manager)
    {


            $user = new User();

            $facture = new Facture();
            $facture->setNumfacture('Numfacture ');
            $facture->setNumtva('Numtva ');
            $facture->setDatefacture(new \DateTime('now'));
            $facture->setVosinfos('Vosinfos ');
            $facture->setInfosclient('Infosclient ');
            $facture->setConditions('Conditions ');
            $facture->setConsignes('Consignes ');
            $facture->setDesignation1('Designation1 ');
            $facture->setQuantite1(5);
            $facture->setPrixht1(150);
            $facture->setTaxe1(12);

            $facture->setDesignation2('Designation2 ');
            $facture->setQuantite2(5);
            $facture->setPrixht2(150);
            $facture->setTaxe2(12);

            $facture->setDesignation3('Designation3 ');
            $facture->setQuantite3(5);
            $facture->setPrixht3(150);
            $facture->setTaxe3(12);

            $user->addFacture($facture);


            $manager->persist($facture);
        //}

        $manager->flush();
    }
}

UserFixtures.php:

<?php

namespace App\DataFixtures;

use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;

class UserFixtures extends Fixture
{
    public function load(ObjectManager $manager)
    {

            $user = new User();
            $user->setName('NameUser');
            $user->setUsername('Username ');
            $user->setPassword('Password');
            $user->setEmail('email@gmail.com');
            $user->getFactures('Factures');

            $manager->persist($user);

        $manager->flush();
    }
}

Ce que je veux

J'aimerais rajouter dans la base de données les factures et les utilisateurs. Comment rajouter les factures en fonction d'un utilisateur ?

Ce que j'obtiens

Anouchka@Anouchka MINGW64 /c/xampp/htdocs/devis_facture (master)
$ php bin/console doctrine:fixtures:load

 Careful, database "devis_facture" will be purged. Do you want to continue? (yes/no) [no]:
 > y

   > purging database
   > loading App\DataFixtures\FactureFixtures

In AbstractMySQLDriver.php line 103:

  An exception occurred while executing 'INSERT INTO user (name, username, password, email, roles) VALUES (?, ?, ?, ?, ?)' with params [null, null, null, null, "a:0:{}"]:

  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null


In PDOStatement.php line 119:

  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null


In PDOStatement.php line 117:

  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null


doctrine:fixtures:load [--append] [--group GROUP] [--em EM] [--shard SHARD] [--purge-with-truncate] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|-
-env ENV] [--no-debug] [--] <command>

1 Réponse

Default
,

Salut pour ton erreur le probleme est que dans ton FactureFixture tu crée un objet user vide (sans aucune proprietes) et ce cette objet vide la que t'essaie de charger en BD.
Pour ressourdre ton probleme essaie ceci
deja pas besoin de 2 fichiers


<?php

namespace App\DataFixtures;

use App\Entity\User;
use App\Entity\Facture;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;

class UserFacturesFixtures extends Fixture
{
public function load(ObjectManager $manager)
{

        $user = new User();
        $user->setName('NameUser');
        $user->setUsername('Username ');
        $user->setPassword('Password');
        $user->setEmail('email@gmail.com');
        $user->getFactures('Factures');
        $manager->persist($user);

        // On associe les factures pour cet utilisateur
        $this->setFacture(user,manager);
        $manager->flush();
}

private function setFacture(User $user, ObjectManager $manager)
{
        $facture = new Facture();
        $facture->setNumfacture('Numfacture ');
        $facture->setNumtva('Numtva ');
        $facture->setDatefacture(new \DateTime('now'));
        $facture->setVosinfos('Vosinfos ');
        $facture->setInfosclient('Infosclient ');
        $facture->setConditions('Conditions ');
        $facture->setConsignes('Consignes ');
        $facture->setDesignation1('Designation1 ');
        $facture->setQuantite1(5);
        $facture->setPrixht1(150);
        $facture->setTaxe1(12);

        $facture->setDesignation2('Designation2 ');
        $facture->setQuantite2(5);
        $facture->setPrixht2(150);
        $facture->setTaxe2(12);

        $facture->setDesignation3('Designation3 ');
        $facture->setQuantite3(5);
        $facture->setPrixht3(150);
        $facture->setTaxe3(12);

        $user->addFacture($facture);              // ou $facture->setUser($user);
        $manager->persist($facture);
    }

}