简体   繁体   English

调用未定义的方法 CodeIgniter\Database\MySQLi\Builder::num_rows()

[英]Call to undefined method CodeIgniter\Database\MySQLi\Builder::num_rows()

Im trying to make a code generator for my table我试图为我的桌子制作一个代码生成器

Here's my model:这是我的 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: 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我想做的是为 nama_barang 的每个项目创建一个 kode_packing_list 生成器,所以当我输入另一件 Renata Blouse 时,它会自动填充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错误调用未定义方法 CodeIgniter\Database\MySQLi\Builder::num_rows() APPPATH\Models\PcuttingModel.php 在第 33 行

and the line 33 is第 33 行是

if ($query->num_rows() <> 0){  

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有关构建器的更多信息,请参阅 CI 文档: https://codeigniter4.github.io/userguide/database/query_builder.ZFC35FDC70D5FC69D269883A822C7A5E3

@SukmaQintara I think this is what you will do. @SukmaQintara 我认为这就是你要做的。 Codeigniter 4 query builder does not really have that method num_row declared but us countResultAll() Codeigniter 4 查询生成器并没有真正声明该方法 num_row 但我们使用 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 answer My last answer OR use the reference document Check this Document如果它不起作用,请尝试我的其他答案我的最后一个答案或使用参考文档检查此文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM