CodeIgniter Forums
CODEIGNITER 4 IN-MODEL VALIDATION AND SAVE() NOT WORKING. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: CODEIGNITER 4 IN-MODEL VALIDATION AND SAVE() NOT WORKING. (/showthread.php?tid=79651)



CODEIGNITER 4 IN-MODEL VALIDATION AND SAVE() NOT WORKING. - jasshh - 07-15-2021

I'm trying to work with CodeIgniter's Model with save(), This is introduced new in the CodeIgniter 4, moreover, when I'm directly calling the below method data is not getting saved in the database
$this->itemModel->save($data)

I'm getting the below error on the browser
ErrorException

Attempt to read property "errno" on bool


<CONTOLLER - INVENTORY.PHP >>

PHP Code:
<?php
namespace App\Controllers;
use 
CodeIgniter\Controller;
use 
App\Models\InventoryModel;

class 
Inventory extends Controller{
    public $itemModel;
    public function __construct(){
        $this->itemModel = new InventoryModel();
        helper(["url","form"]);
    }
  
    
public function addInventory(){
        if($this->request->getMethod()== "post"){
            $data = [
                "name" => $this->request->getVar("itemname",FILTER_SANITIZE_STRING),
                "quantity" => $this->request->getVar("itemquantity"),
                "remarks" => $this->request->getVar("itemremark",FILTER_SANITIZE_STRING)
            ];
            #$this->itemModel->save($data) #DATA NOT GETTING STORED IN DATABASE
            if($this->itemModel->save($data)=== true)
            {
                session()->setTempData("success","Item added to the inventory sucessfully !");
                return redirect()->to(current_url());
            }          
        
}
        return view('inventory',["errors"=>$this->itemModel->errors()]);
    }#EOC AddItem()
          


<<MODEL:- INVENTORYMODEL.PHP >>

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

class 
InventoryModel extends Model {
    protected $table      'inventory';
    protected $primaryKey 'id';
    protected $useAutoIncrement true;
    protected $returnType    'array';
    protected $allowedFields = ['name''quantity','remarks'];

    protected $validationRules    = [
        "itemname" => "required",
        "itemquantity" => "required",
        "itemremarks" => "required"
    ];



<< VIEW: INVENTORY.PHP>>

Code:
<div class="form-panel w3-padding-16 w3-round">
                <?php if (! empty($errors)) : ?>
                <div class="alert alert-danger">
                <?php foreach ($errors as $field => $error) : ?>
                <p><?= $error ?></p>
                <?php endforeach ?>
                </div>
                <?php endif ?>
                <?php if(session()->getTempData("success")):?>
                <?= '<div class="w3-panel w3-green w3-card-4 w3-round w3-padding-16">'.session()->getTempData("success").'</div>' ?>
                <?php endif; ?>
                <?= form_open(current_url(),'role="form" class="form-horizontal style-form"'); ?>
                <div class="form-group">
                    <label class="col-lg-2 control-label"><sup class="w3-text-red">*</sup>Item Name</label>
                  <div class="col-lg-10">
                    <input type="text" placeholder="" id="f-name" class="form-control" name="itemname" >
                  </div>
                </div>
                <div class="form-group">
                  <label class="col-lg-2 control-label"><sup class="w3-text-red">*</sup>Item Quantity</label>
                  <div class="col-lg-10">
                    <input type="number" placeholder=""  class="form-control" min="1" name="itemquantity" >
                  </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-2 control-label">Item Remarks </label>
                  <div class="col-lg-10">
                    <input type="text" placeholder="" id="l-name" class="form-control" name="itemremarks" >
                  </div>
                </div>             
                <div class="form-group">
                  <div class="col-lg-offset-2 col-lg-10">
                      <input type="submit" name="submit" value="Submit" class="btn btn-theme"/>
                      <input type="reset"  value="Clear" class="btn  w3-red"/>
                  </div>
                </div>
            <?= form_close();?>       



RE: CODEIGNITER 4 IN-MODEL VALIDATION AND SAVE() NOT WORKING. - InsiteFX - 07-16-2021

I fixed your post next time please do not use cut copy and paste use the code tags in the editor menu.