【问题标题】:Persist twice on faker with nexest version在最近版本的 faker 上坚持两次
【发布时间】:2020-07-26 07:38:41
【问题描述】:

我在一个 symfony 项目中使用 fzaninotto/faker 的固定装置有问题,我有一些与 ManyToOne 关系的表,当我加载固定装置时无法自动完成,因为我在 composer 上有更新包。

有一个错误和一条消息告诉我添加 cascad persist 但如果我这样做了,则该条目会创建两次。这是我的代码和我的错误信息,对不起我的英语不好......

项目实体:

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @ORM\Table(name="`user`");
 */
class User implements UserInterface, \Serializable
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100, nullable=true)
     */
    private $firstname;

    /**
     * @ORM\Column(type="string", length=100, nullable=true)
     */
    private $lastname;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Role")
     * @ORM\JoinColumn(nullable=false)
     */
    private $role;

角色实体:


<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\RoleRepository")
 */
class Role
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100, unique=true)
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=100, unique=true)
     */
    private $code;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="role")
     */
    private $users;

夹具文件:

<?php

namespace App\DataFixtures;

use Faker\Factory;
use App\Entity\Tag;
use App\Entity\Role;
use App\Entity\User;
use App\Entity\Skill;
use App\Entity\Follow;
use App\Entity\Statut;
use App\Entity\Techno;
use App\Entity\Comment;
use App\Entity\Project;
use App\Entity\Request;
use App\DataFixtures\Provider;
use Faker\ORM\Doctrine\Populator;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

class AppFixtures extends Fixture
{


    public function load(ObjectManager $manager)
    {

        $generator = Factory::create('fr_FR');

        // Ajout provider custom Provider 
        $generator->addProvider(new Provider($generator));

        $populator = new Populator($generator, $manager);


        // REMPLIT LES TABLES SIMPLES

        // table "role"
        $populator->addEntity(
            Role::class,
            2,
            [
                'name' => function () use ($generator) {
                    return $generator->unique()->randomElement(['Administrateur', 'Utilisateur']);
                }
            ]
        );

        // table "user"
        $populator->addEntity(
            User::class,
            10,
            [
                'firstname' => function () use ($generator) {
                    return ($generator->firstName());
                },
                'lastname' => function () use ($generator) {
                    return ($generator->lastName());
                },
            ]
        );

当我加载固定装置时我的错误消息:

In ORMInvalidArgumentException.php line 105:

  Multiple non-persisted new entities were found through the given association graph:                                              

   * A new entity was found through the relationship 'App\Entity\User#role' that was not configured to cascade persist operations  
   for entity: Administrateur. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or conf  
  igure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}).                            
   * A new entity was found through the relationship 'App\Entity\User#role' that was not configured to cascade persist operations  
   for entity: Utilisateur. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configu  
  re cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}).      

【问题讨论】:

    标签: symfony faker


    【解决方案1】:

    与 1.9 版本相同的问题,所以只需降级:

    1/ 删除 vendor 中的 faker 文件夹。

    2/ 从

    更改 composer.json
    "fzaninotto/faker": "^1.9"
    

    "fzaninotto/faker": "1.8"
    

    3/ 运行作曲家更新

    --> 现在呼吸...

    【讨论】:

      猜你喜欢
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      • 1970-01-01
      • 2012-05-12
      • 2021-02-23
      • 2020-08-11
      • 1970-01-01
      相关资源
      最近更新 更多