我在学说中使用了一对多的联接表关联。 Asset
有许多Attribute
s。
我正在尝试在删除资产时让不发布单个属性删除查询的原则。
这是我的测试
$asset = new Asset($assetId = AssetId::generate(), 'foobar');
$attribute = new Attribute((string)AssetId::generate(), rand());
$asset->addAttribute($attribute);
$attribute = new Attribute((string)AssetId::generate(), rand());
$asset->addAttribute($attribute);
$this->em->persist($asset);
$this->em->flush();
$asset = $this->em->getRepository(Asset::CLASS)->find((string)$assetId);
$this->em->remove($asset);
$this->em->flush();
以下是查询原则问题
string(19) ""START TRANSACTION""
string(47) "INSERT INTO attributes (id, name) VALUES (?, ?)"
string(47) "INSERT INTO attributes (id, name) VALUES (?, ?)"
string(43) "INSERT INTO ASSETS (id, name) VALUES (?, ?)"
string(67) "INSERT INTO asset_attributes (asset_id, attribute_id) VALUES (?, ?)"
string(67) "INSERT INTO asset_attributes (asset_id, attribute_id) VALUES (?, ?)"
string(8) ""COMMIT""
string(19) ""START TRANSACTION""
string(31) "DELETE FROM ASSETS WHERE id = ?"
string(35) "DELETE FROM attributes WHERE id = ?"
string(35) "DELETE FROM attributes WHERE id = ?"
string(8) ""COMMIT""
我正试图摆脱针对attributes
表的两个删除查询,而是发出如下所示的单个查询
DELETE FROM attributes where asset_id = ?
我知道doctrine因为cascade: ["all"]
配置而发出了单独的删除,但如果我只是将其设置为persist
,则不会运行删除查询。
这可能吗?
这是我的yml配置
MyCompany\MyProject\Model\Entity\Asset:
type: entity
table: ASSETS
id:
id:
type: string
length: 36
generator:
strategy: none
fields:
name:
type: string
manyToMany:
attributes:
targetEntity: MyCompany\MyProject\Model\Entity\AssetAttribute\Attribute
cascade: ["all"]
fetch: LAZY
orphanRemoval: true
joinTable:
name: asset_attributes
joinColumns:
asset_id:
referencedColumnName: id
onDelete: CASCADE
inverseJoinColumns:
attribute_id:
referencedColumnName: id
unique: true