Skip to content
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

[DataObject] copy/persistence of FieldCollection field between 2 objects #4277

Open
FredoVelcro opened this issue Apr 25, 2019 · 0 comments

Comments

Projects
None yet
1 participant
@FredoVelcro
Copy link

commented Apr 25, 2019

Hello,

I have a Product object with a FieldCollection field (docsList).
Each item is a composed field with a title (string) and a PDF document (Asset\Document).
When trying to simply copy fields between 2 objects, the destination product has the FieldCollection with all items BUT the document object is not linked/set :

// the easy way i would like to do this :
$product2->setDocsList($product1->getDocsList());

I am forced to do a tricky thing like :

$copyDownloadableDocuments = function (Fieldcollection $collection = null) {
                if (!$collection) {
                    return null;
                }

                $docs = new Fieldcollection();
                foreach ($collection as $doc) {
                    /** @var \Pimcore\Model\DataObject\Fieldcollection\Data\DownloadableDocument $doc */
                    $copyDoc = new Fieldcollection\Data\DownloadableDocument();
                    $copyDoc->setDocument($doc->getDocument());
                    $copyDoc->setTitle($doc->getTitle());

                    $docs->add($copyDoc);
                }

                return $docs;
            };

$product2->setDocsList($copyDownloadableDocuments($product1->getDocsList()));

What is the right way to easy copy FieldCollection between 2 products ?
Is it a matter of dirtyMap (already tried...), lazy loading or something else ?

Thank you for any help / good practice to do this.

Best regards,

Fred

@FredoVelcro FredoVelcro changed the title [DataObject] copy/persistence od FieldCollection fields between 2 objects [DataObject] copy/persistence of FieldCollection field between 2 objects Apr 25, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.