CodeIgniter Forums
Why unknow column 'slug_safelink' ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Why unknow column 'slug_safelink' ? (/thread-77917.html)



Why unknow column 'slug_safelink' ? - flux1on - 11-04-2020

I have problem Unknown column 'slug_safelink' in 'where clause'

Controller :
Code:
<?php namespace App\Controllers;
use App\Models\Safelink_model;

class Safelink extends BaseController
{
    public function index()
    {
        /**
        * Load Module
        */
        $Safelink_model = new Safelink_model();

        $data['data'] = $Safelink_model->getData();
        $data['config'] = $this->settings;
        $data['recaptchaWidget'] = $this->recaptcha->getWidget();
        $data['recaptchaScript'] = $this->recaptcha->getScriptTag();
        $data['pesan_gagal'] = $this->session->getFlashdata('inputs');
        $data['error_gagal'] = $this->session->getFlashdata('errors');
        $data['pesan_berhasil'] = $this->session->getFlashdata('success');

        $data['errlog'] = $this->session->getFlashdata('errlog');

        echo view('link/header', $data);
        echo view('link/home', $data);
        echo view('link/footer', $data);
    }

    // Save Data
    public function simpan()
    {
        /**
        * Load Module
        */
        $Safelink_model = new Safelink_model();

        $namaP = esc($this->request->getPost('nama'));
        $slugP = esc($this->request->getPost('slug'));
        $urlP = esc($this->request->getPost('url'));
        $captcha = $this->request->getPost('g-recaptcha-response');

        $validationForm = [
            'nama' => $namaP,
            'slug' => $slugP,
            'url' => $urlP
        ];

        if($this->form_validation->run($validationForm, 'safelink') == FALSE){
            // mengembalikan nilai input yang sudah dimasukan sebelumnya
            $this->session->setFlashdata('inputs', $this->request->getPost());
            // memberikan pesan error pada saat input data
            $this->session->setFlashdata('errors', $this->form_validation->getErrors());
            // kembali ke halaman form
            return redirect()->to(base_url('link'));
        } else {
            $response = $this->recaptcha->verifyResponse($captcha);
            if (isset($response['success']) AND $response['success'] === true) {
                /**
                * Success Verify Anti Bot
                */
                // Apa yang mau disimpan
                $data = [
                    'kode_safelink' => NULL,
                       'nama_safelink' => $namaP,
                       'slug_safelink' => $slugP,
                    'url_safelink' => $urlP,
                    'hits_safelink' => NULL,
                    'created_safelink' => now()
                ];

                $simpan = $Safelink_model->saveData($data);
                    
                if ($simpan) {
                    $this->session->setFlashdata('success', 'Simpan Data Berhasil!');
                    // kembali ke halaman utama
                    return redirect()->to(base_url('link'));
                } else {
                    // memberikan pesan error pada saat input data
                    $this->session->setFlashdata('errors', 'Simpan Data Gagal!');
                    // kembali ke halaman form
                    return redirect()->to(base_url('link'));
                }
            } else {
                // memberikan pesan error pada saat input data
                $this->session->setFlashdata('errors', 'Captcha gagal!');
                // memberikan data input
                $this->session->setFlashdata('inputs', $this->request->getPost());
                // kembali ke halaman form
                return redirect()->to(base_url('link'));
            }
        }
    }
}


Models :
Code:
<?php namespace App\Models;
use CodeIgniter\Model;

class Safelink_model extends Model
{
    protected $table = 'safelink';
    protected $primaryKey = 'kode_safelink';
    protected $allowedFields = ['kode_safelink', 'nama_safelink', 'slug_safelink', 'url_safelink', 'hits_safelink', 'created_safelink'];

    public function goLink($slug)
    {
        $query = $this->db->table($this->table)
                        ->where('slug_safelink', $slug)
                        ->get();

        return $query->getRow();
    }
    
    public function getData($id = null)
    {
        if($id === null){
            return $this->findAll();
        } else {
            return $this->getWhere(['kode_safelink' => $id]);
        }  
    }

    public function saveData($data)
    {
        $query = $this->db->table($this->table)->insert($data);
        return $query;
    }

    public function updateData($data, $id)
    {
        $query = $this->db->table($this->table)->update($data, array('kode_safelink' => $id));
        return $query;
    }

    public function CounterSafelink($data, $id)
    {
        $query = $this->db->table($this->table)->update(array('hits_safelink' => $data+1), array('slug_safelink' => $id));
        return $query;
    }

    public function deleteData($id)
    {
        $query = $this->db->table($this->table)->delete(array('kode_safelink' => $id));
        return $query;
    }
}


Structure Table :
Code:
CREATE TABLE `safelink` (
  `kode_safelink` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `nama_safelink` varchar(80) NOT NULL,
  `slug_safelink` text NOT NULL UNIQUE,
  `url_safelink` text NOT NULL,
  `hits_safelink` bigint NOT NULL DEFAULT '0',
  `created_safelink` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



RE: Why unknow column 'slug_safelink' ? - mlurie - 11-04-2020

Did you initialize the $allowedFields array in your Model?

PHP Code:
protected $allowedFields = ['nama_safelink''slug_safelink''url_safelink''hits_safelink']; 

Also, you don't really need the saveData, updateDate, and deleteData methods if you are using CodeIgniter 4's model.  CodeIgniter already has insert, update, delete, and save methods available to you: https://codeigniter.com/user_guide/models/model.html.


RE: Why unknow column 'slug_safelink' ? - flux1on - 11-04-2020

(11-04-2020, 10:45 AM)mlurie Wrote: Did you initialize the $allowedFields array in your Model?

PHP Code:
protected $allowedFields = ['nama_safelink''slug_safelink''url_safelink''hits_safelink']; 

Also, you don't really need the saveData, updateDate, and deleteData methods if you are using CodeIgniter 4's model.  CodeIgniter already has insert, update, delete, and save methods available to you: https://codeigniter.com/user_guide/models/model.html.

i already add $allowedfields to all models, sry no update in thread

Code:
<?php namespace App\Models;
use CodeIgniter\Model;

class Safelink_model extends Model
{
    protected $table = 'safelink';
    protected $primaryKey = 'kode_safelink';
    protected $allowedFields = ['kode_safelink', 'nama_safelink', 'slug_safelink', 'url_safelink', 'hits_safelink', 'created_safelink'];

    public function goLink($slug)
    {
        $query = $this->db->table($this->table)
                        ->where('slug_safelink', $slug)
                        ->get();

        return $query->getRow();
    }
     
    public function getData($id = null)
    {
        if($id === null){
            return $this->findAll();
        } else {
            return $this->getWhere(['kode_safelink' => $id]);
        }   
    }

    public function saveData($data)
    {
        $query = $this->db->table($this->table)->insert($data);
        return $query;
    }

    public function updateData($data, $id)
    {
        $query = $this->db->table($this->table)->update($data, array('kode_safelink' => $id));
        return $query;
    }

    public function CounterSafelink($data, $id)
    {
        $query = $this->db->table($this->table)->update(array('hits_safelink' => $data+1), array('slug_safelink' => $id));
        return $query;
    }

    public function deleteData($id)
    {
        $query = $this->db->table($this->table)->delete(array('kode_safelink' => $id));
        return $query;
    }
}

i create function saveData,updateData,deleteData for calling function models in controller


RE: Why unknow column 'slug_safelink' ? - mlurie - 11-09-2020

Make sure you spelled 'slug_safelink' the same way in your MySQL database. I just ran into this today.