Welcome Guest, Not a member yet? Register   Sign In
Why - You must set the database table to be used with your query.
#1

(This post was last modified: 01-05-2025, 12:23 AM by 4usol.)

Hi, I wondering about getting this error " You must set the database table to be used with your query."


The marked line is this 
Code:
$var=$this->MyModel ->my_function($vars,$id);


I do that inside a library. The parts of the model are
Code:
namespace App\Models;
use CodeIgniter\Model;

class MyModel extends Model
{
    protected $DB;
     

    #### INIT YOUR MODEL ####
    protected function initialize()
    {
        $this->DB = db_connect('DB');   
        $this->DBA = db_connect('DBA');   
    }

public function my_function($fields,$id)
    {
       
        $qry=$this->DB->table('my_db_table');            //here i define the table, thats why i dont  understand the errormessage!?

        //Select
        if(empty($fields) || in_array('*',$fields)){ $qry->select('*'); }else{ $qry->select($fields); }
       
        //Where
        $qry->Where(['id' => $id]);
     
        //Get the results as RowArray
        $res= $qry->get()->getResultArray();

        //return as array or array of arrays
        if(count($res)>1){ return $res; }else{ return $res[0]; }         
    }

}

may i'm blind to see the missing thing...
Reply
#2

Code:
<?php

namespace App\Models;

use CodeIgniter\Model;
use CodeIgniter\Database\ConnectionInterface;
class BlogModel extends Model
{
    protected $DBGroup          = 'default';
    protected $table            = 'blog';
    protected $primaryKey       = 'Id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
       protected $allowedFields = ['title','article','image','slug','date','font'];
    // Dates
    protected $useTimestamps = false;

if you extend CI4 model , you set field in class definition
CMS CI4 A CMS system, runs out of the box written on top of CI4
Arch Book  CodeIgniter4 on Apache(pages 92-114) 
Reply
#3

(This post was last modified: 01-05-2025, 07:08 AM by 4usol.)

Is it a MUST to define all these "protected ...." settings,
i dont wont to work with the  built-in CRUD methods, so i guess that i dont need to define it!?

I have figure out that the problem comes from another direction, a name of a called function was wrong, if i corret this, the error message (i'm confused about the kind of messag) go away. Now the model works well, without setup the datas on top of the modell.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB