CodeIgniter Forums
SubQuery Not Working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: SubQuery Not Working (/showthread.php?tid=74714)



SubQuery Not Working - louieuow - 10-28-2019

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

Code:
TypeError
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?
Thanx


RE: SubQuery Not Working - louieuow - 11-05-2019

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";


RE: SubQuery Not Working - dhefley - 05-10-2020

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

use CodeIgniter\Database\BaseBuilder;

And it seemed to start working.


RE: SubQuery Not Working - krishnamhnsingh - 05-31-2020

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);
})->get();

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


RE: SubQuery Not Working - basahbasahan - 01-17-2021

(10-28-2019, 03:31 PM)louieuow Wrote: I have the following function that uses a subquery and this error pops up

Code:
TypeError
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?
Thanx


May be it's too late. But you can use "use" in anonymous function if you want to access the variable outside the scope like this :

PHP Code:
$builder->orWhereIn("shop_id",function(BaseBuilder $builder) use ($crsk){
      return $builder->select('shop_id')->from('shops')->where('owner'$crsk);
}); 



RE: SubQuery Not Working - iRedds - 01-18-2021

(01-17-2021, 05:43 PM)basahbasahan Wrote: May be it's too late. But you can use "use" in anonymous function if you want to access the variable outside the scope like this :

PHP Code:
$builder->orWhereIn("shop_id",function(BaseBuilder $builder) use ($crsk){
      return $builder->select('shop_id')->from('shops')->where('owner'$crsk);
}); 

No. The problem is that the hint of the BaseBuilder type is not specified correctly.
Expected

use CodeIgniter\Database\BaseBuilder;


RE: SubQuery Not Working - basahbasahan - 01-29-2021

(05-31-2020, 07:37 PM)krishnamhnsingh Wrote: 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);
                })->get();

        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


I'm answering this question. How to pass parameter to query builder


RE: SubQuery Not Working - paulbalandan - 01-30-2021

(05-31-2020, 07:37 PM)krishnamhnsingh Wrote: 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);
                })->get();

        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

PHP Code:
use CodeIgniter\Database\BaseBuilder;

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

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

        return $quizes;