Reverse side association mapping can not lazy loaded
要特别注意一对一,一对多,等映射关系的 reverse side。因为 reverse side 没有外键,doctrine 无法创建代理对象。
Arbitrary Join
语法和sql类似,把 join - on
改成 join with
Remove OneToMany Collections
If doctrine collection object is owning side, then call collection->clear()
method will also remove the items from
database.
But OneToMany
association is always the reversed side, so It not work.
Set orphanRemoval flag on OneToMany
attribute can fix this, but I don't like it. Because it is implicit
and hard to test. orphanRemoval
delete items one by one:
if ($this->association['type'] & ClassMetadata::TO_MANY &&
$this->association['orphanRemoval'] &&
$this->owner) {
// we need to initialize here, as orphan removal acts like implicit cascadeRemove,
// hence for event listeners we need the objects in memory.
$this->initialize();
foreach ($this->collection as $element) {
$uow->scheduleOrphanRemoval($element);
}
}
Note the flag ClassMetadata::TO_MANY
, it is 12, combine bits 8 and 4, dump it at runtime, it actually maybe 4, so set orphanRemoval
maybe not work.
The same performance as delete one by one in a loop, so here is my way: explicit delete each items using EnittyManager::remove()
method, easier to unit test, stable, and the same performance.
Use Database Reserved Name as FieldName
doctrine
不会自动 escape, 需要手工 escape:
@Column(name="`integer`")
网友评论