CodeIgniter Forums
Upgrade Model CI3 to CI4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Upgrade Model CI3 to CI4 (/showthread.php?tid=78607)



Upgrade Model CI3 to CI4 - cloude - 02-14-2021

Hi all.
I'm trying to upgrade CI3 to CI4 adding namespace and use,
but Visual Studio code get 'Undefined method select'. 

Thanks in advance for help.


PHP Code:
namespace App\Models// CI4

use CodeIgniter\Model// CI4

defined('BASEPATH') OR exit('No direct script access allowed');

class 
myClass extends Model {

    public function myFunc()
    {

        $this->db->select('id, name'); // <-- UNDEFINED METHOD SELECT
        $this->db->from('table');
        $this->db->where('status'1);
        $this->db->where('op''');
        $this->db->group_start();
            $this->db->where('data_cal'NULL);
            $this->db->or_where('data_cal <= NOW()');
        $this->db->group_end();
        $this->db->order_by("id""DESC");

        $query =  $this->db->get()->result();
        
        
if($query) {

            return $query[0];

        } else {
            null;
        }
    }





RE: Upgrade Model CI3 to CI4 - iRedds - 02-14-2021

The $db property contains the connection to the database.
To work with Query Builder you need to get an instance of it.

PHP Code:
$builder $this->db->table('table');
//or
$builder $this->builder('table');
//or
property $table 'table';

$builder $this->builder();

$builder->select(...)->where(...) 

The result() method no longer exists. Use getResult().
Also orWhere, orderBy, groupStart, etc

https://codeigniter.com/user_guide/database/index.html


RE: Upgrade Model CI3 to CI4 - wdeda - 02-14-2021

Just a Model sample
PHP Code:
<?php

namespace App\Models;

use 
CodeIgniter\Model;

class 
SampleModel extends Model
{
    protected $table  'yourtable';
    protected $primaryKey 'id';
        
    
protected $allowedFields = ['status''name']; // just an example

    public function sample()
    {
        $db $this->db;
        $query $db->table('yourtable')
        ->select('id, name')
        ->where('status'1)
        ...
      ->get();
      
      
return $query->getResult(); 
      
    
}




RE: Upgrade Model CI3 to CI4 - Chivinsdev - 02-16-2021

Try this I hope it will help you get what you are looking for



PHP Code:
namespace App\Models;

use 
CodeIgniter\Model;

class 
myClass extends Model {

    public function myFunc()
    {
        
$this->table('table');
        
$this->select('id''name');
        
$this->where(['status' => 1'op' => '']); // ->where('status', 1)->where('op', '');
        -----
        -----
        -----

        $query =  $this->getResult();
    }



I just hope it will help you else call me back to it. I am here to help and also receive help