简体   繁体   English

Phalcon-如何使用Phalcon模型执行SELECT IN子查询?

[英]Phalcon - How do i do a SELECT IN Subquery with Phalcon models?

I need to know how to do i do a subquery type selection with phalcon models? 我需要知道如何使用Phalcon模型进行子查询类型选择?

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) 例如,我想选择所有查看过我的用户,它们存储在UserView表中,列为“ id”,“ user_from”,“ user_to”(由用户表user_id映射到user_from或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 所以我想选择与当前用户一样具有user_to的所有用户,并按user_分组以确保只记录一个,我写了下面的函数来做到这一点,但是有两个基本问题

1. Is how to do sub-query using phalcon models 1.是如何使用Phalcon模型进行子查询

2. Is my logic correctly applied on the back-end of the DB (as i cant see real executed query) 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();
    } 

This returns empty set... 这将返回空集...

So far it is not possible to model subqueries in Phalcon. 到目前为止,尚无法在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 要查询IN ,可以使用queryBuilder

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM