• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Query - Call to undefined method CodeIgniter\Database\MySQLi\Connection::where()

#1
I get this error "Call to undefined method CodeIgniter\Database\MySQLi\Connection::where()" when I try this in my model class

PHP Code:
$this->db->table($table);
        
$this->db->where($where);
        
$this->db->update($save_data); 

however it works fine when

PHP Code:
$this->db->table($table)->where($where)->update($save_data); 

The first approach above worked fine in CI 3. So I suppose in CI 4 I just to have chain or save $db = $this->db;
Reply

#2
(10-04-2018, 01:22 AM)happyape Wrote: I get this error "Call to undefined method CodeIgniter\Database\MySQLi\Connection::where()" when I try this in my model class

PHP Code:
$this->db->table($table);
 
       $this->db->where($where);
 
       $this->db->update($save_data); 

however it works fine when

PHP Code:
$this->db->table($table)->where($where)->update($save_data); 

The first approach above worked fine in CI 3. So I suppose in CI 4 I just to have chain or save $db = $this->db;

This works like says the documentation:

Model.php
PHP Code:
public function updateData (int $id 1) {
    
$this->db->table($this->table)->where('id'$id)->update(
        [
            
'title' => 'New title',
        ]
    );
}

public function 
anotherUpdateData (int $id 1) {
    
$builder $this->db->table($this->table);
    
$builder->where('id'$id);
    
$builder->update(
        [
            
'title' => 'New title 2',
        ]
    );

Reply

#3
(10-04-2018, 04:40 AM)unodepiera Wrote:
(10-04-2018, 01:22 AM)happyape Wrote: I get this error "Call to undefined method CodeIgniter\Database\MySQLi\Connection::where()" when I try this in my model class

PHP Code:
$this->db->table($table);
 
       $this->db->where($where);
 
       $this->db->update($save_data); 

however it works fine when

PHP Code:
$this->db->table($table)->where($where)->update($save_data); 

The first approach above worked fine in CI 3. So I suppose in CI 4 I just to have chain or save $db = $this->db;

This works like says the documentation:

Model.php
PHP Code:
public function updateData (int $id 1) {
 
   $this->db->table($this->table)->where('id'$id)->update(
 
       [
 
           'title' => 'New title',
 
       ]
 
   );
}

public function 
anotherUpdateData (int $id 1) {
 
   $builder $this->db->table($this->table);
 
   $builder->where('id'$id);
 
   $builder->update(
 
       [
 
           'title' => 'New title 2',
 
       ]
 
   );

In CI-4, you can write more simply.

In conttroler
PHP Code:
$data = [
    
'id'=> $id,
    
'title' => 'New title 2'
];
(new \
App\Models\Post)->save($data); 


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

class 
Post extends \CodeIgniter\Model
 
    
protected $table 'Posts';
    protected 
$allowedFields = ['title'];


Reply

#4
(10-04-2018, 01:22 AM)happyape Wrote: I get this error "Call to undefined method CodeIgniter\Database\MySQLi\Connection::where()" when I try this in my model class

PHP Code:
$this->db->table($table);
 
       $this->db->where($where);
 
       $this->db->update($save_data); 

however it works fine when

PHP Code:
$this->db->table($table)->where($where)->update($save_data); 

The first approach above worked fine in CI 3. So I suppose in CI 4 I just to have chain or save $db = $this->db;

A great explanation of Lonnie about this question.
Reply

#5
Yes that makes sense! Thx
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.