doctrine / data-fixtures

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

TopologicalSorter still breaks purge with foreign key constraints #329

Open videni opened 5 years ago

videni commented 5 years ago

I have this issue today at doctrine/data-fixtures v1.3.2, what I did wrong?

Order.orm.xml

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                                      http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <entity name="Acme\Bundle\OrderBundle\Entity\Order" table="acme_order">
        <id name="id" column="id" type="integer">
            <generator strategy="AUTO" />
        </id>
        <one-to-one field="review" target-entity="Acme\Bundle\ReviewBundle\Entity\OrderReview" mapped-by="reviewSubject"/>
        <one-to-one field="report" target-entity="Acme\Bundle\ReportBundle\Entity\Report" mapped-by="order"/>
    </entity>
</doctrine-mapping>

OrderReview.orm.xmln

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping">
    <entity name="Acme\Bundle\ReviewBundle\Entity\OrderReview" table="acme_order_review">
     <id name="id" column="id" type="integer">
            <generator strategy="AUTO" />
        </id>
        <one-to-one field="reviewSubject" inversed-by="review" target-entity="Acme\Bundle\OrderBundle\Entity\Order"/>
    </entity>
</doctrine-mapping>

Report.orm.xml

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                                      http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <entity name="Acme\Bundle\ReportBundle\Entity\Report" table="acme_auto_report"  >
        <id name="id" column="id" type="integer">
            <generator strategy="AUTO" />
        </id>

        <one-to-one field="order" inversed-by="report" target-entity="Acme\Bundle\OrderBundle\Entity\Order" />

    </entity>

</doctrine-mapping>

the the result table orders are following, but acme_auto_report table hold foreign key to acme_order, it must be before the acme_order table like acme_order_review table.

26 => "acme_order_review"
  27 => "acme_order"
  34 => "acme_auto_report"

Originally posted by @videni in https://github.com/doctrine/data-fixtures/issues/230#issuecomment-556507466

videni commented 5 years ago

what quite strange is the error will disapear if call the sort method twice. ORMPurger

 private function getCommitOrder(EntityManagerInterface $em, array $classes)
    {
        $sorter = new TopologicalSorter();

         //....
        $sorter->sort();  // call sort method twice.

        return array_reverse($sorter->sort());
    }