0

No error while I try to insert the data. in fact the session indicator shows that I successfully inserted the data, but when I check the database it was nothing.

 public function save_agenda()
    {
        $idkelas = $this->request->getVar('id_kelas_loop');
        $idtanggalkelasloop = $this->request->getVar('id_tanggal_kelas_loop');
        $namakelas = $this->request->getVar('nama_kls');
        $waktukelas = $this->request->getVar('waktu_kls');
        $statuskelas = $this->request->getVar('status_kelas');

        $tanggalkelas = array(
            'id_tanggal_kelas' => $this->request->getVar('id_tanggal_kelas'),
            'tanggal_kls' => $this->request->getVar('tanggal_kls')
        );
        $this->TanggalKelasModel->save($tanggalkelas);

        for ($kls = 0; $kls < count($namakelas); $kls++) {
            $this->KelasModel->save([
                'id_kelas' => $idkelas[$kls],
                'id_tanggal_kelas' => $idtanggalkelasloop[$kls],
                'nama_kls' => $namakelas[$kls],
                'waktu_kls' => $waktukelas[$kls],
                'status_kls' => $statuskelas[$kls]
            ]);
        }
        session()->setFlashdata('sukses', 'Agenda berhasil diunggah');
        return redirect()->to('/admin/admin/create_agenda');
    }

This is my "KelasModel"

class KelasModel extends Model
{
    protected $table = 'kelas';
    protected $primaryKey  = 'id_kelas';
    protected $allowedFields = [
        'id_kelas', 'id_tanggal_kelas', 'nama_kls', 'waktu_kls', 'status_kls'
    ];
}



class TanggalKelasModel extends Model
{
    protected $table = 'tanggal_kelas';
    protected $primaryKey  = 'id_tanggal_kelas';
    protected $allowedFields = [
        'id_tanggal_kelas', 'tanggal_kls'
    ];
}

This is my "TanggalKelasModel"

<?php

namespace App\Models;

use CodeIgniter\Model;

class TanggalKelasModel extends Model
{
    protected $table = 'tanggal_kelas';
    protected $primaryKey  = 'id_tanggal_kelas';
    protected $allowedFields = [
        'id_tanggal_kelas', 'tanggal_kls'
    ];
}

I have tried to check the allowed fields and the column names are the same.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • you are using 2 models to save data. How do these models look like ? (*minimal code please*) – Vickel May 20 '22 at 20:17
  • yesss im using 2 models, hold i'll send – rayhan aulian May 20 '22 at 20:18
  • please note: no error means save process was correct, but array was most likely empty. You need to debug->what does a `echo '
    ';print_r($tanggalkelas); die;` show for example? And is `$kls < count($namakelas);` really true?
    – Vickel May 20 '22 at 20:27
  • $kls < count($namakelas); this is true, i try the dd($tanggalkelas); and the arrays shows it, it is not empty, and i try to insert alone the tanggalkelas only. i'm not trying the loop and yes even the non loop won;t insert but no error – rayhan aulian May 20 '22 at 20:31
  • or is it the id thing? those 2 models have id as a varchar not auto incrmnt with int data type – rayhan aulian May 20 '22 at 20:43
  • auto-increment integer: https://stackoverflow.com/questions/10447189/int-id-vs-varchar-id, you can always have another column named my_column_specific_id_varchar (if you really need it) – Vickel May 20 '22 at 20:45
  • sorry, i dont understand what you're saying – rayhan aulian May 20 '22 at 20:52

0 Answers0