You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since 2.x of this bundle I noticed strange thing with Auto-Persist objects. If some related object in specific entity (for example ArticleCategory in Article entity) is marked as ORM\ManyToOne(..., cascade: ['persist']) then all another related objects in ArticleCategory will be not Auto-Persisted by Foundry.
Doctrine will generate an Exception like this: Doctrine\ORM\ORMInvalidArgumentException: A new entity was found through the relationship 'App\Entity\ArticleCategory#createdBy' that was not configured to cascade persist operations for entity: App\Entity\User@536. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'App\Entity\User#__toString()' to get a clue.
For example:
// ArticleCategory entityclass ArticleCategory
{
//... some other fields
#[ORM\ManyToOne]
privateUser$createdBy;
}
// Article entityclass Article
{
//... some other fields
#[ORM\ManyToOne(cascade: ['persist'])]
privateArticleCategory$category;
}
// ArticleCategoryFactory factoryclass ArticleCategoryFactory extends PersistentProxyObjectFactory
{
// ...some other stuffprotectedfunctiondefaults(): array|callable
{
return [
'createdBy' => UserFactory::new(),
// ...other fields
];
}
}
// ArticleFactory factoryclass ArticleFactory extends PersistentProxyObjectFactory
{
// ...some other stuffprotectedfunctiondefaults(): array|callable
{
return [
'category' => ArticleCategoryFactory::new(),
// ...other fields
];
}
}
// For example in test class$article = ArticleFactory::new()->create(); // After this will be generated error like "A new entity was found through the relationship....".
I did some digging and I noticed that the problem maybe is in PersistentObjectFactory around line 265. There is a if statment with instruction to "copy" persist behavior from one factory to other:
if ($valueinstanceof self && isset($this->persist)) {
$value->persist = $this->persist; // todo - breaks immutability
}
If I'm not wrong this works like this:
Normalize parameters from ArticleFactory
In ArticleFactory there is a field "category" (relation)
Checks if relation is cascade-persist, if yes then set persist for ArticleCategoryFactory to false (line 298 in PersistentObjectFactory)
Normalize parameters from ArticleCategoryFactory
In ArticleCategoryFactory there is a field createdBy (relation)
Line 265 will be executed and persist-value from ArticleCategoryFactory (before setted to false) will be assigned to UserFactory
If I comment out if statment in line 265 (in PersistentObjectFactory) everything works excellent.
I don't know that is intentional behavior or some bug. On version 1.x everything works well, after upgrade to 2.x is not.
Since 2.x of this bundle I noticed strange thing with Auto-Persist objects. If some related object in specific entity (for example
ArticleCategory
inArticle
entity) is marked asORM\ManyToOne(..., cascade: ['persist'])
then all another related objects inArticleCategory
will be not Auto-Persisted by Foundry.Doctrine will generate an Exception like this:
Doctrine\ORM\ORMInvalidArgumentException: A new entity was found through the relationship 'App\Entity\ArticleCategory#createdBy' that was not configured to cascade persist operations for entity: App\Entity\User@536. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'App\Entity\User#__toString()' to get a clue.
For example:
I did some digging and I noticed that the problem maybe is in
PersistentObjectFactory
around line 265. There is a if statment with instruction to "copy" persist behavior from one factory to other:If I'm not wrong this works like this:
persist
for ArticleCategoryFactory tofalse
(line 298 inPersistentObjectFactory
)createdBy
(relation)ArticleCategoryFactory
(before setted tofalse
) will be assigned toUserFactory
If I comment out if statment in line 265 (in
PersistentObjectFactory
) everything works excellent.I don't know that is intentional behavior or some bug. On version 1.x everything works well, after upgrade to 2.x is not.
There is a Reproducer demo: https://github.com/mleko64/foundry-repro
composer install
bin/phpunit tests/FoundryTest.php
The text was updated successfully, but these errors were encountered: