简体   繁体   中英

Doctrine ODM can't find 'Doctrine\ODM\MongoDB\PersistentCollection' class while updating document

I created a class to update the cart object. When I try to update an object that is already stored in the database, I get an error The class "Doctrine\\ODM\\MongoDB\\PersistentCollection" was not found in the chain configured namespaces App\\Document" . This error occurs only with the cart object (And only during the object update. Creating a new object runs without problems), although there are similar objects in the project that do not cause such a problem.

In addition to this error, I discovered a few more:

  1. This error occurs only when you try to update a field of type "EmbedMany". An attempt to update all other fields passed without errors. Also, any operations with any other objects go without problems.
  2. When I tried to use QueryBuilder, I got the error Class 'Cart' does not exist

Cart class:

namespace App\Document;

use DateTime;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\EmbeddedDocument()
 */
class CartItem
{
    /**
     * @MongoDB\Field(type="string")
     */
    private $productId;

    /**
     * @MongoDB\Field(type="integer")
     */
    private $quantity;

    // More fields ...

    /**
     * @return string
     */
    public function getProductId(): string
    {
        return $this->productId;
    }

    /**
     * @param string $productId
     * @return CartItem
     */
    public function setProductId($productId): CartItem
    {
        $this->productId = $productId;
        return $this;
    }

    /**
     * @return int
     */
    public function getQuantity(): int
    {
        return $this->quantity;
    }

    /**
     * @param int $quantity
     * @return CartItem
     */
    public function setQuantity(int $quantity): CartItem
    {
        $this->quantity = $quantity;
        return $this;
    }

    // More getters and setters ...
}

/**
 * @MongoDB\Document()
 */
class Cart
{
    /**
     * @MongoDB\Id
     */
    private $id;

    /**
     * @MongoDB\Field(type="string")
     */
    private $hash;

    /**
     * @MongoDB\Field(type="date")
     */
    private $date;

    // More fields ...

    /**
     * 'Bad' field
     * @MongoDB\EmbedMany(targetDocument="CartItem")
     */
    private $products;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return string
     */
    public function getHash(): ?string 
    {
        return $this->hash;
    }

    /**
     * @param string $hash
     * @return Cart
     */
    public function setHash(string $hash): Cart
    {
        $this->hash = $hash;
        return $this;
    }

    /**
     * @return DateTime
     */
    public function getDate(): DateTime
    {
        return $this->date;
    }

    /**
     * @param DateTime $date
     * @return Cart
     */
    public function setDate(DateTime $date): Cart
    {
        $this->date = $date;
        return $this;
    }

    // More getters and setters

    /**
     * @return CartItem[]|null
     */
    public function getProducts()
    {
        return $this->products;
    }

    /**
     * @param CartItem[] $products
     * @return Cart
     */
    public function setProducts(array $products): Cart
    {
        $this->products = $products;
        return $this;
    }
}

The class for updating an cart object is just a set of business logic (After checking, it turned out that it does not affect the error in any way, so I don't show the logic) and standard object saving using:

$cart = $this->documentManager->getRepository(Cart::class)->find('some_id');
$cart->setProducts([/* CartItem[] */]);
$this->documentManager->flush();

I also attach the config file ( config/packages/doctrine.yaml ):

doctrine_mongodb:
    auto_generate_proxy_classes: true
    auto_generate_hydrator_classes: true
    connections:
        default:
            server: '%env(resolve:MONGODB_URL)%'
            options: {}
    default_database: '%env(resolve:MONGODB_DB)%'
    document_managers:
        default:
            auto_mapping: true
            mappings:
                App:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Document'
                    prefix: 'App\Document'
                    alias: App

What could be the problem?

Once again checked the returned types of variables at each stage of logic. When retrieving from the database, $cart->getProducts() somehow belongs to the type [object] (Doctrine\\\\ODM\\\\MongoDB\\\\PersistentCollection: {}) , and therefore an error occurs with the class not found when updating the cart object. I also looked at the Doctrine\\Common\\Collections\\Collection interface documentation and found that there is a toArray() method that works fine with the PersistentCollection conversion to [ CartItem[] ] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM