美文网首页
Doctrine Tips

Doctrine Tips

作者: forks1990 | 来源:发表于2020-09-22 09:51 被阅读0次

    Reverse side association mapping can not lazy loaded

    要特别注意一对一,一对多,等映射关系的 reverse side。因为 reverse side 没有外键,doctrine 无法创建代理对象。

    Arbitrary Join

    语法和sql类似,把 join - on 改成 join with

    Arbitrary Join

    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`")
    

    相关文章

      网友评论

          本文标题:Doctrine Tips

          本文链接:https://www.haomeiwen.com/subject/ungiyktx.html