【发布时间】:2019-06-27 12:52:14
【问题描述】:
我创建了一个类来更新购物车对象。当我尝试更新已存储在数据库中的对象时,我收到错误 The class "Doctrine\ODM\MongoDB\PersistentCollection" was not found in the chain configured namespaces App\Document"。此错误仅发生在购物车对象(并且仅在对象更新期间。创建新对象运行没有问题),尽管项目中有类似的对象不会导致此类问题。
除了这个错误,我还发现了几个:
- 仅当您尝试更新“EmbedMany”类型的字段时才会出现此错误。尝试更新所有其他字段,但未出错。此外,对任何其他对象的任何操作都不会出现问题。
- 当我尝试使用 QueryBuilder 时,我收到错误
Class 'Cart' does not exist
购物车类:
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;
}
}
用于更新购物车对象的类只是一组业务逻辑(检查后发现它不会以任何方式影响错误,因此我不显示逻辑)和标准对象保存使用:
$cart = $this->documentManager->getRepository(Cart::class)->find('some_id');
$cart->setProducts([/* CartItem[] */]);
$this->documentManager->flush();
我还附上了配置文件(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
可能是什么问题?
【问题讨论】:
标签: php symfony doctrine doctrine-odm