doctrine-extensions / DoctrineExtensions

Doctrine2 behavioral extensions, Translatable, Sluggable, Tree-NestedSet, Timestampable, Loggable, Sortable
MIT License
4.04k stars 1.27k forks source link

Support for Logging Embedded MongoDB Documents #2169

Closed cnizzardini closed 2 years ago

cnizzardini commented 4 years ago

For me, it wasn't clear if this library supports Logging mongo db embedded documents and I frankly couldn't determine whether or not some of the past PRs added support for this or not. Using gedmo/doctrine-extensions:^3.0 with gedmo/doctrine-extensions:2.1.2 I have a document with an ODM\EmbedMany of ratings. While I can log on the parent document, I get the following error when logging on the embedded documents:

Cannot apply versioning to field [ratings] as it is collection in object - App\Document\Services

<?php

namespace App\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ODM\Document
 * @Gedmo\Loggable
 */
class Services
{
    //use InvestmentTrait;

    /**
     * @ODM\Id(strategy="INCREMENT", type="integer")
     */
     private $id;

    /**
     * @ODM\Field(type="string")
     * @Gedmo\Versioned
     */
    private $title;

    /**
     * @ODM\EmbedMany(targetDocument=Rating::class)
     * @Gedmo\ReferenceMany(type="document", class="Rating", mappedBy="ratings", inversedBy="Services")
     * @Gedmo\Versioned
     */
    private $ratings;

    public function __construct()
    {
        $this->ratings = new ArrayCollection();
    }

    public function setId(int $id)
    {
        $this->id = $id;

        return $this;
    }

    public function getId()
    {
      return $this->id;
    }

    public function getRatings()
    {
        return $this->ratings;
    }

    public function setRatings(array $ratings) : self
    {
        foreach ($ratings as $rating) {
            if (!$rating instanceof Rating) {
                $rating = new Rating($rating);
            }
            $this->addRatings($rating);
        }

        return $this;
    }

    public function addRatings(Rating $rating) : self
    {
        $this->ratings[] = $rating;

        return $this;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }
}

Not sure if this library is meant to support this. I only added Gedmo\ReferenceMany as a troubleshooting step.

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.