CodeIgniter Forums
orderBy did not work when using chunk - 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: orderBy did not work when using chunk (/showthread.php?tid=91725)



orderBy did not work when using chunk - warcooft - 09-26-2024

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



RE: orderBy did not work when using chunk - JustJohnQ - 09-26-2024

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



RE: orderBy did not work when using chunk - warcooft - 09-26-2024

@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.