We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Getting collumn result as array, and then using that array as condition when query.

Hello! :)

I want to return all the statuses of a user and his friends, sort of like a news feed, sorted by earliest posted first. I used to do this in Laravel by creating a array filled with user ids, and then doing a query that returns all statuses where user _ id is one of the ID's from the array.

I can't get the ids properly into a array, and even if I got that to work I have no idea how to do a query where you return all where user_id is equal to one of the ids in the array.

This is how I did it in Laravel:

    public function getFeedForUser(User $user) {
        // Get friends user ID's
        $userIds = $user->followedUsers()->lists('followed_id');

        // Add Users own ID to array
        $userIds[] = $user->id;

        //Return all statuses that contains the given ID's
        return Status::whereIn('user_id', $userIds)->latest()->get();
    }

First I need a array of all the ID's of the users friends. I use a pivot table through a friends.php model to check friendships. Basically I need a query that returns all the friends id in a array.

I'm currently doing somehting like this in Users.php:


        $friendsIDs = $this->getFollowing([
             "columns" => "followed_id"
            ])->toArray();
        var_dump($friendsIDs);

which gives:


array(3) { 
[0]=> array(1) { ["followed_id"]=> string(1) "4" } 
[1]=> array(1) { ["followed_id"]=> string(1) "6" } 
[2]=> string(1) "1" }

Where 4 and 6 are the ID's of the users this user is following. 1 is the ID of the user. As you can see only the users own id is properly inserted as array, not the other two. Don't know how to format it so that I only get the id and not the rest.

Say this works, I then want to use this array as a condition. Basically statuses.php has a user _ id collumn, and I want to return all statuses in order that has one of the ids from that array.

Thank you!!

edited Jun '15

Ok I tried the following in Users.php function:


    public function newsfeed(){
        $IDs = array();
        foreach($this->following as $followed){
             array_push($IDs, $followed->followed_id);
        }

        $IDs[] = $this->id;
        var_dump($IDs);

        return $IDs;
    }

which returns:


array(3) {
[0]=> string(1) "4" 
[1]=> string(1) "6" 
[2]=> string(1) "1" }

So I guess I can use this array now. Not sure why it says int though because ID is defined as INT(10) in the database.

Going to play around with using this array to match records.


        $statuses = Statuses::find([
            "conditions" => "user_id = ?1",
            "bind"       => [1 => $IDs[0]],
            ]);

Works but it only checks for the first value. How do I check against a entire array? If I paste the array I get string to array conversion error.

Closest I come so far is:


        $statuses = Statuses::find([
            "conditions" => "user_id = ?1",
            "bind"       => [1 => implode(" OR ",$IDs)],
            ]);

Only works on the first value in the array, not the rest. I need a beer. :x



11.7k
Accepted
answer

Got it to work, yay!


    public function newsfeed(){
        $IDs = array();
        $statuses = array();
        foreach($this->following as $followed){
             array_push($IDs, $followed->followed_id);
        }
        $IDs[] = $this->id;
        $statuses = Statuses::find([
            "conditions" =>
                "user_id = " . implode(" OR ",$IDs),
            "order" => "id DESC",
                ]);
        return $statuses;
    }