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?

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

so i want to select all the users who has a userto as with the current user, and group by userto 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.userfrom IN :userid: ",array('userid' => $this->user->userid) )

->group('user_id')

->order("XYZ\Models\UsersView.id DESC ")

->limit($limit)

->execute();

}

This returns empty set...



34.7k

Hey

You can take look at https://blog.phalconphp.com/post/phalcon-2-0-3-released to see how to use subqueries