Phalcon-如何使用Phalcon模型执行SELECT IN子查询? [英] Phalcon - How do i do a SELECT IN Subquery with Phalcon models?

查看:685
本文介绍了Phalcon-如何使用Phalcon模型执行SELECT IN子查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何使用Phalcon模型进行子查询类型选择?

例如,我想选择所有查看过我的用户,它们存储在UserView表中,其列为'id','user_from','user_to'(由用户表user_id映射到user_from或user_to)

所以我想选择与当前用户一样具有user_to的所有用户,并按user_分组以确保只记录一个用户,我编写了以下函数来执行此操作,但是存在两个基本问题

1.如何使用Phalcon模型进行子查询

2.我的逻辑是否正确地应用于数据库的后端(因为我看不到实际执行的查询)

public function getUserWithViewedMe($limit=1000000){
        return  User::query()
            ->rightJoin("XYZ\Models\UsersView")
            ->andWhere(" XYZ\Models\UsersView.user_from IN :user_id: ",
                              array('user_id' => $this->user->user_id) )
            ->group('user_id')
            ->order("XYZ\Models\UsersView.id DESC ")
            ->limit($limit)
            ->execute();
    } 

这将返回空集...

解决方案

到目前为止,尚无法在Phalcon中对子查询进行建模.根据标准实施问题,还有主题.

要根据其他表格查询参数,请此处是一个答案.

要查询IN,您可以使用queryBuilder

$this->modelsManager->createBuilder()
    // ...
    ->inWhere('column', $array);

I need to know how to do i do a subquery type selection with phalcon models?

for example i want to select all the users who viewed me, they are stored in the UserView table with columns 'id','user_from','user_to' (mapped by User table user_id to either user_from or user_to)

so i want to select all the users who has a user_to as with the current user, and group by user_to make sure i only get one recorded, I wrote below function to do this but there is fundamental two problems

1. Is how to do sub-query using phalcon models

2. Is my logic correctly applied on the back-end of the DB (as i cant see real executed query)

public function getUserWithViewedMe($limit=1000000){
        return  User::query()
            ->rightJoin("XYZ\Models\UsersView")
            ->andWhere(" XYZ\Models\UsersView.user_from IN :user_id: ",
                              array('user_id' => $this->user->user_id) )
            ->group('user_id')
            ->order("XYZ\Models\UsersView.id DESC ")
            ->limit($limit)
            ->execute();
    } 

This returns empty set...

解决方案

So far it is not possible to model subqueries in Phalcon. There is also topic according to standard implementation issues.

To query params according to other table, here is an answer.

To query IN you can use queryBuilder

$this->modelsManager->createBuilder()
    // ...
    ->inWhere('column', $array);

这篇关于Phalcon-如何使用Phalcon模型执行SELECT IN子查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆