Search code examples
phpcodeigniterpyrocms

How to filter a result returned by a function get_entries() of a stream entries driver in pyrocms?


I have a stream/table named profiles. All of its column are stream-fields. I am trying to restrict the result returned by the the function, get_entries() depending on some criteria. Below is my code:

    $data = [
        'stream'    => 'profiles',
        'namespace' => 'users',
        'where'     => 'user_id = 3'     // lets say, this is my criteria
    ];

    $row = $this->streams->entries->get_entries($data); // returns empty 

The varaible, $row resulted in empty array. Although there is one row in table, profiles where user_id is 3. I have read the documentation of pyrocms and it pretty much says the exact way to use the where clause (just like above).

NOTE: I have also tried writing like

'where' => 'profiles.user_id = 3'`

joy !to avoid table conflict. Still no

But when I write the code like this: $row = $this->streams->entries->get_entries($query);

        $query = [
            'stream'    => 'profiles',
            'namespace' => 'users'     
        ];
        // No where clause this time

        $row = $this->streams->entries->get_entries($query);

This time $row returns all rows including the row with user id 3.

I am unable to use the where clause in get_entries in a right way. I might have done some mistake. Help me out guyz

NOTE: I am using community edition.


Solution

  • I think this might be due to a bug (well, not a bug, but a feature that doesn't work as intended).

    If I'm intentionally issue a wrong query, the sql query output is

    SELECT [ ... ] LEFT JOIN `default_profiles` as `profiles` ON `profiles`.`user_id`=`default_profiles`.`created_by` WHERE (user_id` = 1) ORDER BY `default_profiles`.`created` DESC
    

    Here you see that PyroCMS tries to lookup the data for the "created_by" field. And that doesn't work in this case.

    If you disable the 'created_by' field, you should get the correct row:

    $this->streams->entries->get_entries(
      array(
        'stream' => 'profiles',
        'namespace' => 'users',
        'where' => 'user_id = 3',
        'disable' => 'created_by'
      )
    );
    

    It would be great if you could file an issue on the pyrocms github page. If you won't I'll do it in the next few days.