Search code examples
phpmysqlcodeignitercodeigniter-4

getWhere() must be of the type int or null and 1 more problem CI4


Please kindly to help me with my CI4.

This is the Model in CI4-> ItemsCatModel.php

public function get_items_cat() {
   $builder = $this->table('items_category');
   $query = $this->table('items_category')->orderBy('items_cat_id', 'asc')->getWhere($this->table, array(['items_flag' => 1], ['items_cat_id !=' => 6]));
   return $query->getResultArray();
}

public function get_items_cat_bi($id) {
   $query = $this->query("SELECT * FROM items_category WHERE items_cat_flag = 1 AND items_cat_id <> ' . $id . ' ORDER BY items_cat_id ASC");
   return $query->getResultArray();
    }

when I use the get_items_cat this is the Error pointing at array() Argument 2 passed to CodeIgniter\Database\BaseBuilder::getWhere() must be of the type int or null, array given

and I've tried this query too.. question-> is it the same query with the above query? or is it different?

$query = $this->query("SELECT * FROM items_category WHERE items_cat_flag = 1 AND items_cat_id <> 6 ORDER BY items_cat_id ASC");

if the answer is the yes its same, the query above is running well.

but if i use the query above, when i'm running with my controller it failed.

This is the controller -> Home.php

public function __construct() {
   $this->bannersModel = new BannersModel();
   $this->itemsModel = new ItemsModel();
   $this->itemsCatModel = new ItemsCatModel();
}

public function index() {
   $data['items'] = $this->itemsModel->get_items();
   $data['items_lain'] = $this->itemsModel->get_items_lain();

   $i = 0;
   foreach ($data['item'] as $key => $value) {
      $category = $this->itemsCatModel->get_items_cat_bi($value['items_cat_id']);
      $data['items'][$i]['category'] = $category;
   }
   $i++;
}

and the error was like this Cannot use object of type stdClass as array and this is the error start $category = $this->itemsCatModel->get_items_cat_bi($value['items_cat_id']);

Please kindly to help me to solve the problem. Thank You in advanced.


Solution

  • Explanation:

    PART 1:

    The \CodeIgniter\Model class all userland models extend has no built-in table(...) method. Hence, the line of code below in your custom get_items_cat() method should have thrown an error.

    $builder = $this->table('items_category');

    I assume, you meant to use $builder = $this->db->table('items_category'); instead.

    PART 2:

    $builder->getWhere()

    • Identical to the get() method except that it permits you to add a “where” clause in the first parameter, instead of using the $builder->where() method:

    getWhere

    • Allows the where clause, limit and offset to be added directly.
    public function getWhere(
        $where = null,
        ?int $limit = null,
        ?int $offset = 0,
        bool $reset = true
    )
    

    Based on the references above, the method being discussed expects a $limit which is an int or null. Yet you fed in an array instead (array(['items_flag' => 1], ['items_cat_id !=' => 6])).

    Solution:

    Instead of passing the WHERE condition(s) in the second argument, do that in the first argument instead. I.e:

    // ...
    
    $query = $this->db
        ->table('items_category')
        ->orderBy('items_cat_id', 'ASC')
        ->getWhere(
            [
                ['items_cat_flag =' => 1],
                ['items_cat_id !=' => 6]
            ]
        );
    
    // ...
    

    Addendum

    It also looks like your foreach loop in your index() custom method is erroneous.

    public function index() {
       $data['items'] = $this->itemsModel->get_items();
       // ...
    
       $i = 0;
       foreach ($data['item'] as $key => $value) {
          // ...
       }
       $i++;
    }
    

    I greatly suspect that you meant to have the line of code $i++; placed inside the foreach loop instead. Otherwise, $i will always be set to 0.

    Secondly, you tend to be looping through $data['item'] yet your initial declaration refers to $data['items']. Notice the difference in the pluralization of the word 'item'.