Welcome Guest, Not a member yet? Register   Sign In
orderBy did not work when using chunk
#1

(This post was last modified: 09-26-2024, 04:30 AM by warcooft.)

I have a query using a model to perform chunk operations on data taken from the "konfirmasi_pembayaran" table and perform a join with the "users" table to get the "nama" column. This query also uses orderBy to sort the data by the "user_nama" column (which is an alias for users.nama), but the expected sorting doesn't work well when using the chunk method.

Any help would be greatly appreciated.

The query as follow:

PHP Code:
$order 0;

$model->select('konfirmasi_pembayaran.*, users.nama as user_nama')
    ->join('users''users.id = konfirmasi_pembayaran.user_id''left')
    ->where($where)
    ->orderBy('user_nama''asc')
    ->chunk(100, static function ($confirm) use (&$order$model) {
        $dataToUpdate = [
            'order_id' => ($order++),
        ];
        $model->update($confirm->id$dataToUpdate);
    }); 

Code:
"title": "CodeIgniter\\Database\\Exceptions\\DatabaseException",
"type": "CodeIgniter\\Database\\Exceptions\\DatabaseException",
"code": 500,
"message": "Unknown column 'user_nama' in 'order clause'",

without chunk the query is work fine:

PHP Code:
$dd $model->select('konfirmasi_pembayaran.*, users.nama as user_nama')
            ->join('users''users.id = konfirmasi_pembayaran.user_id''left')
            ->where($where)
            ->orderBy('user_nama''desc')
            ->findAll();

dd($dd); 
@xxxx[{::::::::::::::::::::::::::::::::>
Reply
#2

Try the following code for the orderBy statement:
PHP Code:
->orderBy('users.nama''asc'
Reply
#3

@JustJohnQ Thankyou for your response,
I previously used a method like that but it didn't work, then I asked GPT and recommended using an alias but the result was the same, it didn't work.
@xxxx[{::::::::::::::::::::::::::::::::>
Reply




Theme © iAndrew 2016 - Forum software by © MyBB