关于 php:Doctrine 2: 如何用孩子的数量更新字段? | 珊瑚贝

Doctrine 2: How to update field with number of children?


如果我有两个实体:Parent 和 Child,如何运行单个查询并使用相关子项的数量更新 Parent.number_of_children?

我可以遍历每个父节点,但可以通过 MySQL 中的单个查询轻松完成,问题是如何在 Doctrine 2 中完成(可能使用 DQL)?

  • 使用 @ORM\\PrePersist() 怎么样?


除非有人提供更好的解决方案,否则我会使用原生 SQL:

1
$entityManager -> getConnection() -> query(“…”);

我会使用 PrePersist 和 PreUpdate 事件:

1
2
3
4
5
6
7
8
9
10
11
12
class Parent
{
    /**
     * @ORM\\PrePersist()
     * @ORM\\PreUpdate()
     */

    public function onPrePersistAndUpdate()
    {
        $this->numberOfChildren = count($this->getChildren());
    }

}

  • 是的,但是如果我有 10000 个父母和 10000 个孩子呢?使用本机 MySQL 查询,我可以在不从数据库中提取任何数据的情况下做到这一点……


来源:https://www.codenong.com/9649278/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_10106.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?