• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SubQuery Not Working

I have the following function that uses a subquery and this error pops up

Argument 1 passed to App\Models\MainModel::App\Models\{closure}() must be an instance of App\Models\BaseBuilder,
instance of CodeIgniter\Database\MySQLi\Builder given, called in C:\CI_system_4A\Database\BaseBuilder.php on line 970

PHP Code:
function get_current_subjects()
        $db = \Config\Database::connect();
        $builder $db->table('central.v1_cal_subjects');
        $builder $builder->distinct()->select("subject_code,subject_name");
        $builder $builder->whereIn("cal_id", function(BaseBuilder $builder)   [u][color=#e82a1f]// error on this line[/color][/u]
                                                    return $builder->select("cal_id")->from('central.v1_calendar')->where('`year` >= year(now())'null,false); 
        $builder $builder->orderby("subject_code");
        $query $builder->get();
        return $query->getResultArray();

I used the example for the Online reference as a guide

Quote:$builder->whereIn('id', function(BaseBuilder $builder) {
    return $builder->select('job_id')->from('users_jobs')->where('user_id', 3);
// Produces: WHERE "id" IN (SELECT "job_id" FROM "users_jobs" WHERE "user_id" = 3)

An ideas on whats wrong with my code?

No answers yet!

My debugging leads me to think that the issue is actually with the CI4 System framework itself. It appears not to be building the subquery at all, before it hands the result back from the anonymous function call [function(BaseBuilder $builder)].

I have given up on subqueries an just using the SQL code below

$sql = "SELECT DISTINCT subject_code,subject_name
FROM central.v1_cal_subjects
WHERE cal_id IN (SELECT cal_id FROM central.v1_calendar WHERE `year` >= YEAR(NOW()))
ORDER BY subject_code";

Same issue here with upgrade to 4.0.3, but I added:

use CodeIgniter\Database\BaseBuilder;

And it seemed to start working.

What if i want to pass arguments to this function

function getQuizes($course_id) {
$db = db_connect();
$builder = $db->table('quiz_courses');

$quizes = $this->whereIn('id', function(BaseBuilder $builder,$course_id) {
return $builder->select('quiz_id')->distinct()->from('quiz_courses')->where('course_id', $course_id);

return $quizes;

getting error

Too few arguments to function App\Models\QuizModel::App\Models\{closure}(), 1 passed in C:\wamp64\www\ci-news\vendor\codeigniter4\framework\system\Database\BaseBuilder.php on line 1032 and exactly 2 expected

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.