Welcome Guest, Not a member yet? Register   Sign In
SubQuery Not Working
#1

(This post was last modified: 10-29-2019, 07:18 AM by PaulD. Edit Reason: Added [php][/php] tags around your code blocks )

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
Reply
#2

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";
Reply
#3

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

use CodeIgniter\Database\BaseBuilder;

And it seemed to start working.
Reply
#4

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
Reply
#5

(This post was last modified: 01-17-2021, 05:44 PM by basahbasahan.)

(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);
}); 
Reply
#6

(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;
Reply
#7

(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
Reply
#8

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

Reply




Theme © iAndrew 2016 - Forum software by © MyBB