完全无关的异常,同时保存多对多关系原则和php
因此,我的应用程序中有多个实体,它们之间: 产品*-*订单 用户*-1用户角色 命令 产品完全无关的异常,同时保存多对多关系原则和php,php,orm,doctrine,Php,Orm,Doctrine,因此,我的应用程序中有多个实体,它们之间: 产品*-*订单 用户*-1用户角色 命令 产品 class Product { ... /** * @ManyToMany(targetEntity="Order", mappedBy="products") */ protected Collection $orders; ... public function __construct() { $this->or
class Product
{
...
/**
* @ManyToMany(targetEntity="Order", mappedBy="products")
*/
protected Collection $orders;
...
public function __construct()
{
$this->orders = new ArrayCollection();
}
public function addOrder(Order $order): self
{
$this->orders[] = $order;
return $this;
}
}
使用者
和用户角色:
class UserRole
{
/** @Id @Column(type="integer") @GeneratedValue */
protected $id;
/** @Column(type="string", nullable=false, unique=true, length=20) */
protected $name;
/** @Column(type="integer", unique=true, nullable=false) */
protected $rolePriority;
/**
* Many Users have Many Stores.
* @ManyToMany(targetEntity="UserRoleAction", inversedBy="user_role_actions", fetch="EAGER", cascade={"persist"})
*/
protected Collection $userRoleActions;
/**
* UserRole constructor.
*/
public function __construct()
{
$this->userRoleActions = new ArrayCollection();
}
...
}
好的,到目前为止,当我尝试保存包含许多选定产品的新订单时,我的问题如下:
class OrderService
{
public function createFromRequest(HttpRequest $request)
{
...
$products = $this->entityManager->getRepository(Product::class)->findWhatever(); //products are coming from this without issues.
foreach ($products as $product) {
$order->addProduct($product);
}
$this->entityManager->persist($order);
$this->entityManager->flush();
return $order;
}
}
我得到:
Blockquote关联字段“User#$role”的类型“UserRole”的预期值改为“u PHP_complete_Class”
我的意思是,这与其他实体有什么关系???我不知道该去哪里公平。有什么建议吗?条令是否在幕后进行了一些完全无关的验证?什么
class UserRole
{
/** @Id @Column(type="integer") @GeneratedValue */
protected $id;
/** @Column(type="string", nullable=false, unique=true, length=20) */
protected $name;
/** @Column(type="integer", unique=true, nullable=false) */
protected $rolePriority;
/**
* Many Users have Many Stores.
* @ManyToMany(targetEntity="UserRoleAction", inversedBy="user_role_actions", fetch="EAGER", cascade={"persist"})
*/
protected Collection $userRoleActions;
/**
* UserRole constructor.
*/
public function __construct()
{
$this->userRoleActions = new ArrayCollection();
}
...
}
class OrderService
{
public function createFromRequest(HttpRequest $request)
{
...
$products = $this->entityManager->getRepository(Product::class)->findWhatever(); //products are coming from this without issues.
foreach ($products as $product) {
$order->addProduct($product);
}
$this->entityManager->persist($order);
$this->entityManager->flush();
return $order;
}
}