php - Call to undefined method CodeIgniterDatabaseMySQLiBuilder::num_rows()

Im trying to make a code generator for my table
Here's my model :
public function buat_kode() {
$this->db->table('RIGHT(proses_cutting.kode_packing_list,6) as kode', FALSE);
$this->builder()->orderBy('kode_packing_list','DESC');
$this->builder()->limit(1);
$query = $this->db->table('proses_cutting'); //cek dulu apakah ada sudah ada kode di tabel.
if ($query->num_rows() <> 0){
//jika kode ternyata sudah ada.
$data = $query->row();
$kode = intval($data->kode) + 1;
}
else {
//jika kode belum ada
$kode = 1;
}
$kodemax = str_pad($kode, 6, "0", STR_PAD_LEFT); // angka 4 menunjukkan jumlah digit angka 0
$kodejadi = "PLIK059".$kodemax; // hasilnya ODJ-9921-0001 dst.
return $kodejadi;
}
controller :
public function create()
{
session();
$data = [
'main' => 'prosescutting/create',
'validation' => \Config\Services::validation(),
'title' => 'Form Tambah Proses Cutting',
'kodeunik' => $this->pcuttingModel->buat_kode(),
'kode_packing_list' => $kodePackingSekarang,
];
return view('template/template', $data);
}
and my table : Table
What im trying to do is make a generator for kode_packing_list for each item of nama_barang so when i input another Renata Blouse it will automatically fill with PLIK059000007
But when I tried to run it, it show me these errors:
Error Call to undefined method CodeIgniter\Database\MySQLi\Builder::num_rows() APPPATH\Models\PcuttingModel.php at line 33
and the line 33 is
if ($query->num_rows() <> 0){
Answer
Solution:
You are using the wrong database table functions, try this:
$db = \Config\Database::connect(); // optional; init database if not created yet
$builder = $db->table('proses_cutting');
$builder->select('RIGHT(proses_cutting.kode_packing_list,6) as kode');
$builder->orderBy('kode_packing_list','DESC');
$builder->limit(1);
if($builder->countAllResults() > 0) {
$query = $builder->get();
$result = $query->getResult(); // Result as objects eg; $result->kode
$kode = $result->kode;
}
// Other option: I believe you can do this too
$builder = $db->table('proses_cutting');
$builder->select('RIGHT(proses_cutting.kode_packing_list,6) as kode');
$builder->orderBy('kode_packing_list','DESC');
$builder->limit(1);
$query = $builder->get();
$result = $query->getResult('array');
if(is_array($result) && count($array) > 0) {
$kode = $result['kode'];
}
For more information about the builder, see the CI documentation: https://codeigniter4.github.io/userguide/database/query_builder.html
Answer
Solution:
@SukmaQintara I think this is what you will do. Codeigniter 4 query builder does not really have that method num_row declared but us countResultAll()
Example
$query = $this->db->table('proses_cutting'); //cek dulu apakah ada sudah ada kode di tabel.
if ($query->countResultAll() <> 0){
//jika kode ternyata sudah ada.
$data = $query->row();
$kode = intval($data->kode) + 1;
}
else {
//jika kode belum ada
$kode = 1;
}
If it does not work try my other answerMy last answer OR use the reference document Check this Document
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: apache/2.4.52 (win64) openssl/1.1.1m php/8.1.2 server at localhost port 80
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.