Welcome Guest, Not a member yet? Register   Sign In
Upgrade Model CI3 to CI4
#1

(This post was last modified: 02-14-2021, 04:57 AM by cloude.)

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;
        }
    }


Reply
#2

(This post was last modified: 02-14-2021, 09:38 AM by iRedds.)

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
Reply
#3

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(); 
      
    
}

Reply
#4

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB