我在symfony项目中使用fzaninotto/faker的fixture上遇到问题,我有一些关系为ManyToOne的表,由于我在composer上更新了捆绑包,所以在我加载fixture时无法自动完成。
有一个错误和一条消息告诉我添加cascad持久存在,但如果我这样做,条目将创建两次。这是我的代码和错误信息,很抱歉我的英语不好。。。
项目主体:
<?php
namespace AppEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use SymfonyComponentSecurityCoreUserUserInterface;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass="AppRepositoryUserRepository")
* @ORMTable(name="`user`");
*/
class User implements UserInterface, Serializable
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="string", length=100, nullable=true)
*/
private $firstname;
/**
* @ORMColumn(type="string", length=100, nullable=true)
*/
private $lastname;
/**
* @ORMManyToOne(targetEntity="AppEntityRole")
* @ORMJoinColumn(nullable=false)
*/
private $role;
角色实体:
<?php
namespace AppEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass="AppRepositoryRoleRepository")
*/
class Role
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="string", length=100, unique=true)
*/
private $name;
/**
* @ORMColumn(type="string", length=100, unique=true)
*/
private $code;
/**
* @ORMOneToMany(targetEntity="AppEntityUser", mappedBy="role")
*/
private $users;
夹具文件:
<?php
namespace AppDataFixtures;
use FakerFactory;
use AppEntityTag;
use AppEntityRole;
use AppEntityUser;
use AppEntitySkill;
use AppEntityFollow;
use AppEntityStatut;
use AppEntityTechno;
use AppEntityComment;
use AppEntityProject;
use AppEntityRequest;
use AppDataFixturesProvider;
use FakerORMDoctrinePopulator;
use DoctrineBundleFixturesBundleFixture;
use DoctrineCommonPersistenceObjectManager;
use SymfonyComponentSecurityCoreEncoderUserPasswordEncoderInterface;
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 'AppEntityUser#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 'AppEntityUser#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"}).
1.9版本也有同样的问题,所以只是降级:
1/删除供应商中的faker文件夹。
2/从更改composer.json
"fzaninotto/faker": "^1.9"
至
"fzaninotto/faker": "1.8"
3/运行composer更新
-->现在呼吸。。。