-
-
Notifications
You must be signed in to change notification settings - Fork 328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
3.x — as no identity/no id values set. It cannot be added to the identity map. #777
Comments
That sounds weird. There is indeed a difference at the persistence level for Alice 3.x: it has been moved to theofidry/AliceDataFixtures, but as you can see, it doesn't do much: https://github.com/theofidry/AliceDataFixtures/blob/master/src/Loader/PersisterLoader.php#L77. Did you use a special config option in 2.x or something? |
Nope, nothing special other than Custom Faker providers, which was easily switched over with new implementation. Not sure what to do for above error, which is very important to fix. Any suggestions? |
I've never seen that error before, did you find a way to reproduce it somewhere? |
I have reproduced the problem at https://github.com/iBasit/alice-problem.git Getting up and running is easy as 123.
and then just test with I hope you can help on this. Thank you |
Any luck how I can fix this? |
Soz I'm a bit busy this w/we with my moving I couldn't check yet. |
Off Topic Back on Topic |
Thanks I'll check it out :)
I'll try to have a proper look in the evening, but otherwise it will take longer. Is it expected that |
Thanks. Yes detail is using parent primary key as the primary key of detail, rather than its own. It's one to one relationship. |
I did have a look at the doctrine2 issue before I posted here, they head phpdoc error. I was trying to dig in and I found the error is generated by following line I'm not sure how to fix this issue, this issue did not exist previously on 2.x |
Ok I pinned down the issue. The issue is really what is described in the StackOverflow question. In you case what you need to do in raw PHP is: $manager->persist($user);
$manager->flush();
$manager->persist($detail);
$manager->flush(); Now to make it work, there is several solutions. A first one would be to create your own use AppBundle\Entity\Detail;
use Fidry\AliceDataFixtures\LoaderInterface;
use Fidry\AliceDataFixtures\Persistence\PersisterAwareInterface;
use Fidry\AliceDataFixtures\Persistence\PersisterInterface;
use Fidry\AliceDataFixtures\Persistence\PurgeMode;
use Fidry\AliceDataFixtures\ProcessorInterface;
use Nelmio\Alice\IsAServiceTrait;
/**
* Loader decorating another loader to add a persistence layer.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Théo FIDRY <theo.fidry@gmail.com>
*
* @final
*/
/*final*/ class PersisterLoader implements LoaderInterface, PersisterAwareInterface
{
use IsAServiceTrait;
/**
* @var LoaderInterface
*/
private $loader;
/**
* @var PersisterInterface
*/
private $persister;
/**
* @var ProcessorInterface[]
*/
private $processors;
/**
* @param LoaderInterface $decoratedLoader
* @param PersisterInterface $persister
* @param ProcessorInterface[] $processors
*/
public function __construct(LoaderInterface $decoratedLoader, PersisterInterface $persister, array $processors)
{
$this->loader = $decoratedLoader;
$this->persister = $persister;
$this->processors = (function (ProcessorInterface ...$processors) {
return $processors;
})(...$processors);
}
/**
* @inheritdoc
*/
public function withPersister(PersisterInterface $persister): self
{
return new self($this->loader, $persister, $this->processors);
}
/**
* Pre process, persist and post process each object loaded.
*
* {@inheritdoc}
*/
public function load(array $fixturesFiles, array $parameters = [], array $objects = [], PurgeMode $purgeMode = null): array
{
$objects = $this->loader->load($fixturesFiles, $parameters, $objects, $purgeMode);
foreach ($objects as $id => $object) {
if ($object instanceof Detail) {
continue; // Persist it at the second pass
}
foreach ($this->processors as $processor) {
$processor->preProcess($id, $object);
}
$this->persister->persist($object);
}
$this->persister->flush();
foreach ($objects as $id => $object) {
if (!$object instanceof Detail) {
continue;
}
foreach ($this->processors as $processor) {
$processor->preProcess($id, $object);
}
$this->persister->persist($object);
}
$this->persister->flush();
foreach ($objects as $id => $object) {
foreach ($this->processors as $processor) {
$processor->postProcess($id, $object);
}
}
return $objects;
}
} As you can see there is two A cleaner way although a bit more hacky woud be to create your own persister and skip the This IMO is a workaround however. It would be nice to tackle the issue at the core, but it requires to considerate more complex scenarios where you have a chain of 1to1 relationships for example. |
Also note that if it was working in 2.x, that was by cheer luck. |
You missed that bit:
But I would rather recommend the second solution |
I have overwritten the class, but on run time, it loads the overwritten class and then after loading all the datafixtures I have committed the changes. Do you think there is still some other bug? |
I need to take a proper look but I can't right now. If you override the service (i.e. declare you own service with the same service ID) it should work though. |
I agree, If you check following service file which I committed on the the alice-problem git repo, it has same service id. it loads fine on first load, but on second load it takes the old service.. service names |
What do you mean by second load? Just in case also double check you deleted the container |
Sorry, what I mean is when it runs |
Sorry I forgot to post my message yesterday, see theofidry/alice-problem@14bf2e0. Arguably there is some design issues:
Can't do much atm, but PRs are welcome :) |
Thank you so much, this helped a lot. I had an issue with purger also, but manage to fix it. |
Let's keep the issue open as they are some changes to be made to make it easier :) |
Great! — Just heads up, if there is a relationship in sync with detail to other entities, this fix will effect them also, so one would have to persist them as well. My example was also on the note, |
Moved the issue to the right repository: theofidry/AliceDataFixtures#50 :) |
Here's a quick solution: Edit the fixtures loader to skip certain keys, like this embeddable Infrastructure\Persistence\Doctrine\Extension\Point:
local__blablabla:
__construct: [53.2164587474, -0.9746272299] private function persistFixtures(array $fixtures, ObjectManager $objectManager)
{
$loader = new AppNativeLoader();
$objectSet = $loader->loadFiles($fixtures);
foreach ($objectSet->getObjects() as $key => $object)
{
$this->addReference($key, $object);
// skip keys that starts with 'local__' mainly used for embeddables
if ($this->isLocal($key) === false)
{
$objectManager->persist($object);
}
}
// Note: the flashing happens later when all fixtures (ORM/ODM) are persisted
}
private function isLocal($key) : bool
{
return strpos($key, 'local__') === 0;
} Here I choose |
@Mahmoudz do note however that embeddables are properly ignored already. If that's not the case then it's a bug. |
I have two entities.
user (entity 1)
detail (entity 2)
When running fixtures with
I get the following error
Please note that this was working fine on
2.x
Stackoverflow links
https://stackoverflow.com/a/30404503/75799
The text was updated successfully, but these errors were encountered: