CodeIgniter Forums
Error trying to use query builder (version 4.04) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Error trying to use query builder (version 4.04) (/showthread.php?tid=77136)



Error trying to use query builder (version 4.04) - mongoose1130 - 07-23-2020

I'm receiving this error:
  • Argument 1 passed to App\Models\PostModel::App\Models\{closure}() must be an instance of App\Models\BaseBuilder

When I try to use the "orWhereIn" method of the query builder like so:

$results = $this->select('id, body, timestamp, user_id')
                       ->where('user_id', $userID)
                       ->orWhereIn('id', function(BaseBuilder $subq) {
                               return $subq->select('followed_id')
                                                  ->from('followers')
                                                  ->where('follower_id', $userID);
                               })
                        ->orderBy('timestamp', 'DESC')
                        ->findAll();


The "$this" above is in context with the model I am in, so I am using the built-in query builder instance of the model. I have the initial table set up by a "protected $table" declaration (thus no from clause under the first select).

Not sure how to fix it - can anyone help?


RE: Error trying to use query builder (version 4.04) - InsiteFX - 07-24-2020

The builtin instance of the Query Build is $this->builder();


RE: Error trying to use query builder (version 4.04) - mongoose1130 - 07-24-2020

Thanks, but I tried that and am still getting the same error.  Huh

new code:

$builder = $this->builder();       
$builder->select('id, body, timestamp, user_id');
$builder->where('user_id', $userID);
$builder->orWhereIn('id', function(BaseBuilder $builder)
                          {
                              return $builder->select('followed_id')
                                              ->from('followers')
                                              ->where('follower_id', $userID);
                          });
$builder->orderBy('timestamp', 'DESC');
$results = $builder->get();


RE: Error trying to use query builder (version 4.04) - InsiteFX - 07-24-2020

This is strange, $this->builder is an instance of BaseBuilder.

You may have found a bug here, I would report it on GitHub.


RE: Error trying to use query builder (version 4.04) - Gary - 09-01-2020

I'd suggest perhaps trying to give the handle to the closure's function a different name to separate it from the first one... they both seem to be $builder (?)... maybe?


RE: Error trying to use query builder (version 4.04) - paulbalandan - 09-01-2020

You are missing the use statement at the top of your class name for the BaseBuilder. If you will look at the error, the base builder is namespaced using App\\Models.