Home > Enterprise >  Why does cloning a Doctrine entity with associations result in one extra INSERT?
Why does cloning a Doctrine entity with associations result in one extra INSERT?

Time:01-29

I have two related entities as follows:

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <entity name="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkOrders" table="app-name-here.rmtk_orders">
        <indexes>
            <index name="incoterms_id" columns="incoterms_id"/>
            <index name="slots_id" columns="slots_id"/>
            <index name="statuses_id" columns="statuses_id"/>
            <index name="restaurant_users_id" columns="supplier_users_id"/>
            <index name="origins_id" columns="origins_id"/>
            <index name="payment_code" columns="payment_code"/>
            <index name="currencies_iso_code" columns="currencies_iso_code"/>
            <index name="domains_id" columns="domains_id"/>
            <index name="dispatch_countries_iso_code_2" columns="dispatch_countries_iso_code_2"/>
            <index name="subpayment_code" columns="subpayment_code"/>
            <index name="language_code" columns="language_code"/>
            <index name="users_id" columns="users_id"/>
        </indexes>
        <unique-constraints>
            <unique-constraint name="orders_id2" columns="orders_id2,supplier_users_id,splitter"/>
        </unique-constraints>
        <id name="ordersId" type="integer" column="orders_id">
            <generator strategy="IDENTITY"/>
        </id>
        <field name="ordersId2" type="string" column="orders_id2" length="36" nullable="true">
            <options>
                <option name="fixed"/>
            </options>
        </field>
        <field name="username" type="string" column="username" length="256" nullable="false">
            <options>
                <option name="fixed"/>
            </options>
        </field>
        <field name="createdTs" type="datetime" column="created_ts" nullable="false"/>
        <field name="lastUpdate" type="datetime" column="last_update" nullable="true">
            <options>
                <option name="default">CURRENT_TIMESTAMP</option>
            </options>
        </field>
        <field name="purchasedTs" type="datetime" column="purchased_ts" nullable="false"/>
        <field name="remoteAddr" type="string" column="remote_addr" length="45" nullable="true">
            <options>
                <option name="fixed"/>
            </options>
        </field>
        <field name="comments" type="text" column="comments" length="65535" nullable="true">
            <options>
                <option name="fixed"/>
            </options>
        </field>
        <field name="channel" type="string" column="channel" length="16" nullable="true">
            <options>
                <option name="fixed"/>
            </options>
        </field>
        <field name="hidden" type="boolean" column="hidden" nullable="false">
            <options>
                <option name="default">0</option>
            </options>
        </field>
        <field name="version" type="string" column="version" length="3" nullable="true">
            <options>
                <option name="fixed">1</option>
            </options>
        </field>
        <field name="userAgent" type="string" column="user_agent" length="256" nullable="true">
            <options>
                <option name="fixed"/>
            </options>
        </field>
        <field name="backend" type="string" column="backend" length="32" nullable="true">
            <options>
                <option name="fixed"/>
            </options>
        </field>
        <field name="referralCode" type="string" column="referral_code" length="6" nullable="true">
            <options>
                <option name="fixed"/>
            </options>
        </field>
        <field name="hasSettlementsAttributed" type="boolean" column="has_settlements_attributed" nullable="false">
            <options>
                <option name="default">0</option>
            </options>
        </field>
        <field name="totalVerified" type="boolean" column="total_verified" nullable="true"/>
        <field name="totalVerificationTs" type="datetime" column="total_verification_ts" nullable="true"/>
        <field name="nonViesTaxId" type="string" column="non_vies_tax_id" length="32" nullable="true">
            <options>
                <option name="fixed"/>
            </options>
        </field>
        <field name="vatNumber" type="string" column="vat_number" length="16" nullable="true">
            <options>
                <option name="fixed"/>
            </options>
        </field>
        <field name="vatNumberAppliedFromScVatTaxReport" type="boolean"
               column="vat_number_applied_from_sc_vat_tax_report" nullable="false">
            <options>
                <option name="default">0</option>
            </options>
        </field>
        <field name="vatNumberAppliedFromVidr" type="boolean" column="vat_number_applied_from_vidr" nullable="false">
            <options>
                <option name="default">0</option>
            </options>
        </field>
        <field name="vatNumberViesValidity" type="boolean" column="vat_number_vies_validity" nullable="true"/>
        <field name="vatNumberViesValidityVerificationTs" type="datetime"
               column="vat_number_vies_validity_verification_ts" nullable="true"/>
        <field name="exposedViaApi" type="boolean" column="exposed_via_api" nullable="false">
            <options>
                <option name="default">0</option>
            </options>
        </field>
        <field name="acknowledged" type="boolean" column="acknowledged" nullable="false">
            <options>
                <option name="default">0</option>
            </options>
        </field>
        <field name="notifyFailedAmazonInvoiceUploads" type="boolean" column="notify_failed_amazon_invoice_uploads"
               nullable="false">
            <options>
                <option name="default">0</option>
            </options>
        </field>
        <field name="dispatchCountriesIsoCode2AppliedFromScVatTaxReport" type="boolean"
               column="dispatch_countries_iso_code_2_applied_from_sc_vat_tax_report" nullable="false">
            <options>
                <option name="default">0</option>
            </options>
        </field>
        <field name="dispatchCountriesIsoCode2AppliedFromVidr" type="boolean"
               column="dispatch_countries_iso_code_2_applied_from_vidr" nullable="false">
            <options>
                <option name="default">0</option>
            </options>
        </field>
        <field name="magentoEntityId" type="integer" column="magento_entity_id" nullable="true">
            <options>
                <option name="unsigned"/>
            </options>
        </field>
        <field name="onbuyInternalReference" type="integer" column="onbuy_internal_reference" nullable="true">
            <options>
                <option name="unsigned"/>
            </options>
        </field>
        <field name="invoiceUploadedSpApi" type="boolean" column="invoice_uploaded_sp_api" nullable="false">
            <options>
                <option name="default">0</option>
            </options>
        </field>
        <field name="kauflandUploaded" type="integer" column="kaufland_uploaded" nullable="true">
            <options>
                <option name="unsigned"/>
            </options>
        </field>
        <field name="piiPurged" type="boolean" column="pii_purged" nullable="false">
            <options>
                <option name="default">0</option>
            </options>
        </field>
        <field name="billingYear" type="smallint" column="billing_year" nullable="true">
            <options>
                <option name="unsigned"/>
            </options>
        </field>
        <field name="billingMonth" type="boolean" column="billing_month" nullable="true"/>
        <field name="splitter" type="integer" column="splitter" nullable="false">
            <options>
                <option name="default">1</option>
            </options>
        </field>
        <field name="dontInvoice" type="boolean" column="dont_invoice" nullable="false">
            <options>
                <option name="default">0</option>
            </options>
        </field>
        <field name="kauflandIdOrderUnit" type="string" column="kaufland_id_order_unit" length="32" nullable="true">
            <options>
                <option name="default">0</option>
            </options>
        </field>

        <many-to-one field="dispatchCountriesIsoCode2" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkCountries"
                     fetch="LAZY">
            <join-columns>
                <join-column name="dispatch_countries_iso_code_2" referenced-column-name="countries_iso_code_2"
                             nullable="true"/>
            </join-columns>

        </many-to-one>
        <many-to-one field="currenciesIsoCode" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkCurrencies"
                     fetch="LAZY">
            <join-columns>
                <join-column name="currencies_iso_code" referenced-column-name="currencies_iso_code"/>
            </join-columns>

        </many-to-one>
        <many-to-one field="languageCode" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkLanguages2"
                     fetch="LAZY">
            <join-columns>
                <join-column name="language_code" referenced-column-name="language_code"/>
            </join-columns>

        </many-to-one>
        <many-to-one field="users" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkUsers" fetch="LAZY">
            <join-columns>
                <join-column name="users_id" referenced-column-name="users_id"/>
            </join-columns>

        </many-to-one>
        <many-to-one field="slots" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkSlots" fetch="LAZY">
            <join-columns>
                <join-column name="slots_id" referenced-column-name="slots_id" nullable="true"/>
            </join-columns>

        </many-to-one>
        <many-to-one field="incoterms" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkIncoterms" fetch="LAZY">
            <join-columns>
                <join-column name="incoterms_id" referenced-column-name="incoterms_id"/>
            </join-columns>

        </many-to-one>
        <many-to-one field="supplierUsers" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkUsers" fetch="LAZY">
            <join-columns>
                <join-column name="supplier_users_id" referenced-column-name="users_id"/>
            </join-columns>

        </many-to-one>
        <many-to-one field="paymentCode" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkPaymentMethods"
                     fetch="LAZY">
            <join-columns>
                <join-column name="payment_code" referenced-column-name="payment_code"/>
            </join-columns>

        </many-to-one>
        <many-to-one field="origins" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkOrdersOrigins" fetch="LAZY">
            <join-columns>
                <join-column name="origins_id" referenced-column-name="origins_id"/>
            </join-columns>

        </many-to-one>
        <many-to-one field="domains" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkDomains" fetch="LAZY">
            <join-columns>
                <join-column name="domains_id" referenced-column-name="domains_id"/>
            </join-columns>

        </many-to-one>
        <many-to-one field="statuses" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkStatuses" fetch="LAZY">
            <join-columns>
                <join-column name="statuses_id" referenced-column-name="statuses_id"/>
            </join-columns>

        </many-to-one>
        <many-to-one field="subpaymentCode" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkSubpaymentMethods"
                     fetch="LAZY">
            <join-columns>
                <join-column name="subpayment_code" referenced-column-name="subpayment_code"/>
            </join-columns>

        </many-to-one>
        <many-to-many field="invoice" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkInvoices" fetch="LAZY">
            <join-table name="app-name-here.rmtk_invoices">
                <join-columns>
                    <join-column name="orders_id" referenced-column-name="orders_id"/>
                </join-columns>
                <inverse-join-columns>
                    <join-column name="invoices_id" referenced-column-name="invoices_id"/>
                </inverse-join-columns>
            </join-table>

        </many-to-many>

        <many-to-many field="addresses" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkAddresses" fetch="LAZY">
            <join-table name="app-name-here.rmtk_addresses">
                <join-columns>
                    <join-column name="orders_id" referenced-column-name="orders_id"/>
                </join-columns>
                <inverse-join-columns>
                    <join-column name="addresses_id" referenced-column-name="addresses_id" />
                </inverse-join-columns>
            </join-table>
            <cascade>
                <cascade-all/>
            </cascade>
        </many-to-many>

        <many-to-many field="tangibleItems" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkOrdersProducts"
                      fetch="LAZY">
            <join-table name="app-name-here.rmtk_orders_products">
                <join-columns>
                    <join-column name="orders_id" referenced-column-name="orders_id"/>
                </join-columns>
                <inverse-join-columns>
                    <join-column name="orders_products_id" referenced-column-name="orders_products_id"/>
                </inverse-join-columns>
            </join-table>
            <cascade>
                <cascade-all/>
            </cascade>
        </many-to-many>

        <many-to-many field="nonTangibleItems"
                      target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkOrdersNontangibles" fetch="LAZY">
            <join-table name="app-name-here.rmtk_orders_nontangibles">
                <join-columns>
                    <join-column name="orders_id" referenced-column-name="orders_id"/>
                </join-columns>
                <inverse-join-columns>
                    <join-column name="on_id" referenced-column-name="on_id" unique="true"/>
                </inverse-join-columns>
            </join-table>
            <cascade>
                <cascade-all/>
            </cascade>
        </many-to-many>

        <many-to-many field="mwsMfnRecords" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkAmazonOrdersMfn"
                      fetch="LAZY">
            <join-table name="app-name-here.rmtk_amazon_orders_mfn">
                <join-columns>
                    <join-column name="order_id" referenced-column-name="orders_id2"/>
                </join-columns>
                <inverse-join-columns>
                    <join-column name="id" referenced-column-name="id"/>
                </inverse-join-columns>
            </join-table>

        </many-to-many>

        <many-to-many field="mwsAfnRecords" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkAmazonOrdersAfn"
                      fetch="LAZY">
            <join-table name="app-name-here.rmtk_amazon_orders_afn">
                <join-columns>
                    <join-column name="amazon_order_id" referenced-column-name="orders_id2"/>
                </join-columns>
                <inverse-join-columns>
                    <join-column name="id" referenced-column-name="id"/>
                </inverse-join-columns>
            </join-table>

        </many-to-many>

        <many-to-many field="spApiAfnRecords"
                      target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkAmazonSpApiOrdersAfn" fetch="LAZY">
            <join-table name="app-name-here.rmtk_amazon_sp_api_orders_afn">
                <join-columns>
                    <join-column name="amazon_order_id" referenced-column-name="orders_id2"/>
                </join-columns>
                <inverse-join-columns>
                    <join-column name="id" referenced-column-name="id"/>
                </inverse-join-columns>
            </join-table>

        </many-to-many>

        <many-to-many field="vidrRecords" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkAmazonSpApiVidr"
                      fetch="LAZY">
            <join-table name="app-name-here.rmtk_amazon_sp_api_vidr">
                <join-columns>
                    <join-column name="order_id" referenced-column-name="orders_id2"/>
                </join-columns>
                <inverse-join-columns>
                    <join-column name="vidr_id" referenced-column-name="vidr_id"/>
                </inverse-join-columns>

            </join-table>

        </many-to-many>

        <many-to-many field="scVatTaxReports"
                      target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkAmazonSpApiScVatTaxReports" fetch="LAZY">
            <join-table name="app-name-here.rmtk_amazon_sp_api_sc_vat_tax_reports">
                <join-columns>
                    <join-column name="order_id" referenced-column-name="orders_id2"/>
                </join-columns>
                <inverse-join-columns>
                    <join-column name="sc_id" referenced-column-name="sc_id"/>
                </inverse-join-columns>
            </join-table>

        </many-to-many>

    </entity>
</doctrine-mapping>

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <entity name="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkOrdersNontangibles"
            table="app-name-here.rmtk_orders_nontangibles">
        <indexes>
            <index name="settlement_id" columns="settlement_id"/>
            <index name="orders_id" columns="orders_id"/>
        </indexes>
        <unique-constraints>
            <unique-constraint name="idx_key_id" columns="orders_id,nontangibles_key,nontangibles_subkey"/>
        </unique-constraints>
        <id name="onId" type="integer" column="on_id">
            <generator strategy="IDENTITY"/>
        </id>
        <field name="nontangiblesKey" type="string" column="nontangibles_key" length="64" nullable="true">
            <options>
                <option name="fixed"/>
            </options>
        </field>
        <field name="nontangiblesSubkey" type="string" column="nontangibles_subkey" length="64" nullable="true">
            <options>
                <option name="fixed"/>
            </options>
        </field>
        <field name="nontangiblesPrice" type="decimal" column="nontangibles_price" precision="15" scale="4"
               nullable="false"/>
        <field name="nontangiblesTaxRate" type="decimal" column="nontangibles_tax_rate" precision="7" scale="4"
               nullable="true"/>
        <field name="nontangiblesTaxClass" type="integer" column="nontangibles_tax_class" nullable="true">
            <options>
                <option name="unsigned"/>
            </options>
        </field>
        <field name="nontangiblesQuantity" type="decimal" column="nontangibles_quantity" precision="15" scale="4"
               nullable="false">
            <options>
                <option name="default">1.0000</option>
            </options>
        </field>
        <field name="issuedcreditnote" type="boolean" column="issuedCreditNote" nullable="false">
            <options>
                <option name="default">0</option>
            </options>
        </field>
        <field name="isfromamazonsettlement" type="boolean" column="isFromAmazonSettlement" nullable="false">
            <options>
                <option name="default">0</option>
            </options>
        </field>
        <field name="ebayReferenceId" type="integer" column="ebay_reference_id" nullable="true">
            <options>
                <option name="unsigned"/>
            </options>
        </field>
        <many-to-one field="orders" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkOrders" fetch="LAZY">
            <join-columns>
                <join-column name="orders_id" referenced-column-name="orders_id"/>
            </join-columns>

        </many-to-one>
        <many-to-one field="settlement" target-entity="ISV-Here\App-Name-Here\Doctrine\Entities\RmtkAmazonSettlements"
                     fetch="LAZY">
            <join-columns>
                <join-column name="settlement_id" referenced-column-name="settlement_id"/>
            </join-columns>

        </many-to-one>
    </entity>
</doctrine-mapping>

The end goal is to clone an instance of ISV-Here\App-Name-Here\Doctrine\Entities\RmtkOrders along with its associations. I have implemented __clone() as follows:

class Orders
{
.....
    public function __clone()
        {
            if ($this->ordersId) {
                unset($this->ordersId);

                $nonTangibleItems = new ArrayCollection();

                foreach ($this->nonTangibleItems as $nonTangibleItem) {
                    $nonTangibleItemClone = clone $nonTangibleItem;
                    $nonTangibleItemClone->setOrder($this);
                    $nonTangibleItems->add($nonTangibleItemClone);
                }

                $this->nonTangibleItems = $nonTangibleItems;
            }
        }
    }
}

My controller is as follows:

        /**
         * @var RmtkOrders $rmtkOrder
         */
        $rmtkOrder = $this->entityManager->find(DOCTRINE_ENTITIES_NAMESPACE . 'RmtkOrders', 123456);
        $rmtkOrder->setOrdersId2('das' . time());
        $rmtkOrder->setInvoice(null);
        $rmtkOrder->setTangibleItems(new ArrayCollection());
        $rmtkOrder->setVidrRecords(new ArrayCollection());
        $rmtkOrder->setAddresses(new ArrayCollection());
        $rmtkOrder->setScVatTaxReports(new ArrayCollection());
        $rmtkOrder->setSpApiAfnRecords(new ArrayCollection());

        $copy = clone $rmtkOrder;
        $this->getEntityManager()->persist($copy);

        try {
            $this->getEntityManager()->flush($copy);
        } catch (Exception $ex) {
            Cli::doOutputLine($ex->getMessage());
        }

The MariaDB general log shows the following INSERT statements:

            2506366 Query   INSERT INTO app-name-here.rmtk_orders (orders_id2, username, created_ts, last_update, purchased_ts, remote_addr, comments, channel, hidden, version, user_agent, backend, referral_code, has_settlements_attributed, total_verified, total_verification_ts, non_vies_tax_id, vat_number, vat_number_applied_from_sc_vat_tax_report, vat_number_applied_from_vidr, vat_number_vies_validity, vat_number_vies_validity_verification_ts, exposed_via_api, acknowledged, notify_failed_amazon_invoice_uploads, dispatch_countries_iso_code_2_applied_from_sc_vat_tax_report, dispatch_countries_iso_code_2_applied_from_vidr, magento_entity_id, onbuy_internal_reference, invoice_uploaded_sp_api, kaufland_uploaded, pii_purged, billing_year, billing_month, splitter, dont_invoice, kaufland_id_order_unit, dispatch_countries_iso_code_2, currencies_iso_code, language_code, users_id, slots_id, incoterms_id, supplier_users_id, payment_code, origins_id, domains_id, statuses_id, subpayment_code) VALUES ('das1674928808', '[email protected]', '2023-01-28 19:16:48', '2023-01-25 03:22:06', '2023-01-24 00:54:39', NULL, NULL, 'AFN', 0, NULL, NULL, NULL, NULL, 0, 1, '2023-01-28 19:16:50', NULL, '', 1, 0, NULL, NULL, 1, 0, 0, 1, 0, NULL, NULL, 0, NULL, 0, NULL, NULL, 1, 0, NULL, 'FR', 'EUR', 'fr_FR', 260636, 744311, NULL, 55999, 'amazon', 5, 2, 73, NULL)
            2506366 Query   INSERT INTO app-name-here.rmtk_orders_nontangibles (nontangibles_key, nontangibles_subkey, nontangibles_price, nontangibles_tax_rate, nontangibles_tax_class, nontangibles_quantity, issuedCreditNote, isFromAmazonSettlement, ebay_reference_id, orders_id, settlement_id) VALUES ('shipping', NULL, '0', '20', NULL, '1', 0, 0, NULL, 1893942, NULL)
            2506366 Query   INSERT INTO app-name-here.rmtk_orders_nontangibles (nontangibles_key, nontangibles_subkey, nontangibles_price, nontangibles_tax_rate, nontangibles_tax_class, nontangibles_quantity, issuedCreditNote, isFromAmazonSettlement, ebay_reference_id, orders_id, settlement_id) VALUES ('shipping_discount', NULL, '0', '20', NULL, '1', 0, 0, NULL, 1893942, NULL)
            2506366 Query   INSERT INTO app-name-here.rmtk_orders_nontangibles (nontangibles_key, nontangibles_subkey, nontangibles_price, nontangibles_tax_rate, nontangibles_tax_class, nontangibles_quantity, issuedCreditNote, isFromAmazonSettlement, ebay_reference_id, orders_id, settlement_id) VALUES ('promotion_discount', NULL, '0', '20', NULL, '1', 0, 0, NULL, 1893942, NULL)
            2506366 Query   INSERT INTO app-name-here.rmtk_orders_nontangibles (orders_id, on_id) VALUES (1893942, 1091050)

The very last INSERT statement leads to An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1091053' for key 'PRIMARY'. All the rest of the INSERT statements are exactly as desired.

The very last INSERT statement should not be there at all. Could you spot what the problem is and why this last INSERT statement is executed?

CodePudding user response:

Did you try after clone to detach from the entity manager your new instance ?

 $this->getEntityManager()->detach($copy);

CodePudding user response:

I'm not sure to understand your problem, because it sounds like code works (and fails) exactly as expected. As you are cloning RmtkOrders, this is a new entity that doctrine have to persist.

When an object is cloned, a new one is setup with the same properties. So, it fails because the initial entity already has an id and the cloned one duplicate this id. This id violates the unique integrity constraints of primary key when Doctrine try to store it.

In my opinion, if you want to create a new entity and store it, you should implement the magic method __clone() to unset RmtkOrders::id

public function __clone()
{
     $this->id = null;
}

Now doctrine will autogenerate a new id on persist.

Optionally, if needed, you perhaps have to clone the subentity in the same way.

public function __clone()
{
     $this->id = null;
     //the below code is necessary because __construct is NOT called by __clone
     $this->setInvoice(null);
     $this->setTangibleItems(new ArrayCollection());
     $this->setVidrRecords(new ArrayCollection());
     $this->setAddresses(new ArrayCollection());
     $this->setScVatTaxReports(new ArrayCollection());
     $this->setSpApiAfnRecords(new ArrayCollection());
}

and your controller becomes:

        /**
         * @var RmtkOrders $rmtkOrder
         */
        $rmtkOrder = $this->entityManager->find(DOCTRINE_ENTITIES_NAMESPACE . 'RmtkOrders', 123456);
        $copy = clone $rmtkOrder;
        $copy->setOrdersId2('das' . time()); //I don't understand this line, it could be move to __clone method
        $this->getEntityManager()->persist($copy);

        try {
            $this->getEntityManager()->flush($copy);
        } catch (Exception $ex) {
            Cli::doOutputLine($ex->getMessage());
        }

In any case, I strongly advice you to never update $rmtkOrder, but only manipulate the cloned $clone. And I suggest you to use Model pattern if the magic __clone method is already used for another use case.

  • Related