原则:本机查询的自动结果映射?

Doctrine: Automatic ResultMapping of Native Query?

经过一些研究,我在我的存储库中使用了这个 UNION 查询 class:

class PostRepository extends ServiceEntityRepository {

    // ...

    public function getLatestPostsOfUser ($limit = 10) : ?array {

        $sql = <<<SQL
SELECT p.id, p.title, p.content, p.images, p.parent_id, p.parent_type, p.created, p.last_modified FROM cms_posts p
LEFT JOIN cms_user_follow ON (p.parent_type = cms_user_follow.followed_entity_type AND p.parent_id = cms_user_follow.followed_entity_id) 
WHERE cms_user_follow.user_id = {$this->currentUser->getId()}

UNION 

SELECT p.id, p.title, p.content, p.images, p.parent_id, p.parent_type, p.created, p.last_modified FROM cms_posts p
LEFT JOIN project_memberships ON (p.parent_type = 'Project' AND p.parent_id = project_memberships.project_id) 
WHERE project_memberships.user_id = {$this->currentUser->getId()} and project_memberships.status = 1

ORDER BY created DESC
LIMIT $limit
SQL;

        $res = [];

        try {
            $rsm = (new ResultSetMapping())
                ->addEntityResult(Post::class, 'p')
                ->addFieldResult('p', 'id', 'id')
                ->addFieldResult('p', 'title', 'title')
                ->addFieldResult('p', 'content', 'content')
                ->addFieldResult('p', 'images', 'images')
                ->addFieldResult('p', 'parent_id', 'parentId')
                ->addFieldResult('p', 'parent_type', 'parentType')
                ->addFieldResult('p', 'created', 'created')
                ->addFieldResult('p', 'last_modified', 'lastModified')
            ;

            $res = $this->getEntityManager()->createNativeQuery($sql, $rsm)->getArrayResult();
        } catch (DBALException $e) {

        }

        return $res;
    }

}

它涉及大量的手动字段映射,所以我想知道是否有自动解决方案?

非常感谢!

看起来 Doctrine 可以在幕后做类似的事情并且还可以自动应用映射:

$qb = $this->em->createQueryBuilder();

$where = $qb->expr()->andX(
    $qb->expr()->eq('f.followedEntityId', ':parentId'),
    $qb->expr()->eq('f.followedEntityType', ':parentType'),
    $qb->expr()->eq('f.following', 1),
    $qb->expr()->eq('u.allowEmailNotifications', 1),
    $qb->expr()->eq('u.enabled', 1),
);

$qb->select('f', 'u')
    ->from(UserFollow::class, 'f')
    ->leftJoin(
        User::class,
        'u',
        Join::WITH,
        'f.userId = u.id'
    )
    ->where($where)
    ->setParameters([
        'parentId' => $post->getParentId(),
        'parentType' => $post->getParentType()
    ])
;

$usersAndFollowings = $qb->getQuery()->getResult();

$usersAndFollowings 是一个平面数组,两个实体交替出现:[UserFollow, User, UserFollow, User, ...]

您可能希望之后对其进行处理,以便连接的 UserFollow 和 User 实体一起在一个子数组中。