Doctrine 2 ManyToMany cascade(Doctrine 2 多对多级联)
问题描述
是否可以在 Doctrine 2 中创建两个多对多相关的对象并仅在其中一个上调用 persist 以保存两者?
Is it possible in Doctrine 2 to create two objects that are many to many related and call persist only on one of them to save both?
用户实体:
/**
* Owning Side
*
* @ManyToMany(targetEntity="Role", inversedBy="users", cascade={"persist"})
* @JoinTable(name="user_roles",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="role_id", referencedColumnName="id")}
* )
*/
public $roles;
角色实体:
/**
* Inverse Side
*
* @ManyToMany(targetEntity="User", mappedBy="roles")
*/
public $users;
保存:
$role = new Role();
$user = new User();
$user->roles->add($role);
$role->users->add($user);
$em->persist($user);
$em->flush();
它不起作用并引发错误通过未配置为级联持久化操作的关系找到新实体:EntitiesRole@0000000004a29c11000000005c48cb75.显式持久化新实体或在关系上配置级联持久化操作."
It doesn't work and trows an error "A new entity was found through a relationship that was not configured to cascade persist operations: EntitiesRole@0000000004a29c11000000005c48cb75. Explicitly persist the new entity or configure cascading persist operations on the relationship."
推荐答案
您应该将 cascade={"persist"}
应用于 Role 实体.
You should apply cascade={"persist"}
to the Role entity.
不是 Doctrine 专家,但我认为 Doctrine 检查关联实体的级联选项.
Not an expert on Doctrine, but I think Doctrine checks the associated entity for cascading options.
由于您将持久从 用户 到 角色,它会检查角色 实体,如果它应该被级联持久化.
Since you are cascading the persist from Users to Roles, it checks the Role entity if it should be persisted with cascade.
这篇关于Doctrine 2 多对多级联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Doctrine 2 多对多级联
基础教程推荐
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01