<?xml version="1.0" encoding="UTF-8" ?>

<!--

 This file is part of the Sylius package.

 (c) Sylius Sp. z o.o.

 For the full copyright and license information, please view the LICENSE
 file that was distributed with this source code.

-->

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
    xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping">

    <mapped-superclass name="Sylius\Component\Product\Model\ProductAssociation" table="sylius_product_association">
        <unique-constraints>
            <unique-constraint columns="product_id,association_type_id" name="product_association_idx" />
        </unique-constraints>

        <id name="id" column="id" type="integer">
            <generator strategy="AUTO" />
        </id>

        <field name="createdAt" column="created_at" type="datetime">
            <gedmo:timestampable on="create"/>
        </field>
        <field name="updatedAt" column="updated_at" type="datetime" nullable="true">
            <gedmo:timestampable on="update"/>
        </field>

        <many-to-one field="type" target-entity="Sylius\Component\Product\Model\ProductAssociationTypeInterface">
            <join-columns>
                <join-column name="association_type_id" referenced-column-name="id" nullable="false" on-delete="CASCADE" />
            </join-columns>
        </many-to-one>

        <many-to-one field="owner" target-entity="Sylius\Component\Product\Model\ProductInterface" inversed-by="associations">
            <join-columns>
                <join-column name="product_id" referenced-column-name="id" nullable="false" on-delete="CASCADE" />
            </join-columns>
        </many-to-one>

        <many-to-many field="associatedProducts" target-entity="Sylius\Component\Product\Model\ProductInterface">
            <order-by>
                <order-by-field name="id" />
            </order-by>

            <join-table name="sylius_product_association_product">
                <join-columns>
                    <join-column name="association_id" referenced-column-name="id" nullable="false" unique="false" on-delete="CASCADE" />
                </join-columns>
                <inverse-join-columns>
                    <join-column name="product_id" referenced-column-name="id" nullable="false" unique="false" on-delete="CASCADE" />
                </inverse-join-columns>
            </join-table>
        </many-to-many>

    </mapped-superclass>
</doctrine-mapping>
