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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 01:47:50

问题


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...


回答1:


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);


来源:https://stackoverflow.com/questions/33772911/phalcon-how-do-i-do-a-select-in-subquery-with-phalcon-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!