doctrine / data-fixtures

Doctrine2 ORM Data Fixtures Extensions
http://www.doctrine-project.org
MIT License
2.78k stars 224 forks source link

Attributes are not populated with sharing objects #249

Closed Kwadz closed 7 years ago

Kwadz commented 7 years ago

When I reference an object which has attributes populated:

screen_shot_2017-01-18_at_16_45_11

The attribute values disappear (except id) when I get the reference:

screen_shot_2017-01-18_at_16_44_22

stof commented 7 years ago

This is because you are getting a non-initialized proxy (which will get initialized when you call anything else that getId() on it, because each fixture is loaded separately and the EM is cleared between them (avoiding to do a flush on a huge unit of work the last time). There is nothing wrong here.

Kwadz commented 7 years ago

How do you suggest to initialize the proxy?

class ProcessorDataLoader extends AbstractFixture implements OrderedFixtureInterface
{
    public function load(ObjectManager $manager) {
        $myProcessor = new Processor();
        $myProcessor->setFactory("PaypalTransactionProcessor");
        $myProcessor->setName("PaypalTransactionProcessor");

        $manager->persist($myProcessor);
        $manager->flush();

        $this->addReference('my-processor', $myProcessor);
    }
}
class TransactionDataLoader extends AbstractFixture implements OrderedFixtureInterface
{
    private $maVar;
    public function load(ObjectManager $manager) {
        $transaction = new PaymentTransaction();
        $processor = $this->getReference("my-processor");
        $transaction->setProcessor($processor);
        $transaction->setAmount(60);
        $transaction->setCurrency("EUR");

        $manager->persist($transaction);
        $manager->flush();
    }
}
mikeSimonson commented 7 years ago

@Kwadz You typically initialize a proxy by calling a getter on a not initialized property (not the id). But it doesn't matter in your case for your code to work you only need the id of your processor.

Close as invalid. Don't hesitate to reopen if you have clarification about an issue.

Kwadz commented 7 years ago

Do you suggest initializing the proxy by calling a getter like this:

class ProcessorDataLoader extends AbstractFixture implements OrderedFixtureInterface
{
    public function load(ObjectManager $manager) {
        $myProcessor = new Processor();
        $myProcessor->setFactory("PaypalTransactionProcessor");
        $myProcessor->setName("PaypalTransactionProcessor");

        $myProcessor->getName();  // DO WE HAVE TO INITIALIZE LIKE THIS?

        $manager->persist($myProcessor);
        $manager->flush();

        $this->addReference('my-processor', $myProcessor);
    }
}
mikeSimonson commented 7 years ago

You don't need to initialize it. As soon as your code will need something from that processor it will initialize itself automatically, you don't need to do anything. The only reason why it's not initialized yet is because no part of your code needed it.

Kwadz commented 7 years ago

In all cases it should be at least usable by the other fixtures during the creation. And here it is not the case.

I made a new test. I realized the problem occurs during the second pass (many fixtures need the hotel-item-group reference).

First pass:

capture_d_ecran_12_02_2017_01_54

Second pass:

capture_d_ecran_12_02_2017_01_56

So, I get:

[PDOException]
  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'item_group_id' cannot be null
Kwadz commented 7 years ago

To reproduce the error, I updated this repo which contains everything needed. You just need to create the db and execute DefaultControllerTest::testLoadFixtures().

Could you reopen the issue until we fixed it?

Kwadz commented 7 years ago

Don't hesitate to reopen if you have clarification about an issue.

@mikeSimonson I can't repoen the issue as it's not me who closed it. Can you reopen it please ?

Kwadz commented 7 years ago

In fact a @ORM\JoinColumn name was wrong. The issue came from the Offer entity configuration.