Welcome Guest, Not a member yet? Register   Sign In
CI4 How to load a model by sending params
#1

I have a controller that loads a model and I need to pass parameters
PHP Code:
namespace App\Controllers;


class 
Utenti_interni extends BaseController
{
    private $query_add$final_query$sql;


    public function __construct()
    {
        $this->Mymodel model('App\Models\Mymodel');

    }



How can I send parameters to the model?
I tried with:

PHP Code:
$this-Mymodel model('App\Models\Mymodel'true, ['123''abc']); 
But it does not work
Reply
#2

(This post was last modified: 02-14-2022, 04:51 AM by demyr.)

On top of your controller call your Model
PHP Code:
use App\Models\YourFolderNameForModels\UsersModel//for example user models 


You can call it in your __construct like you did, but be careful:

PHP Code:
$this->UserModel = new UsersModel(); 

Then you can use it :

Code:
$data = [
'name' => $name_from_the_form,
'surname' => $surname_from_the_form
];

$result = $this->UsersModel->lets_do_something($data);
Reply
#3

(02-14-2022, 04:50 AM)demyr Wrote: On top of your controller call your Model
PHP Code:
use App\Models\YourFolderNameForModels\UsersModel//for example user models 


You can call it in your __construct like you did, but be careful:

PHP Code:
$this->UserModel = new UsersModel(); 

Then you can use it :

Code:
$data = [
'name' => $name_from_the_form,
'surname' => $surname_from_the_form
];

$result = $this->UsersModel->lets_do_something($data);

So yes, I would like to pass parameters to the class instantiation using
PHP Code:
model('mymodel'); 
Reply
#4

Sadly, you can't. `model()`'s 2nd and 3rd parameters are fixed to accept instances of ConnectionInterface and ValidationInterface.
https://github.com/codeigniter4/CodeIgni...#L809-L812
Reply
#5

(This post was last modified: 02-15-2022, 12:24 AM by kenjis.)

2nd parameter is $getShared.
3rd parameter is ConnectionInterface.

Do you want to do like this?
Mymodel does not extend CodeIgniter\Model.

PHP Code:
$this->Mymodel = new \App\Models\Mymodel('123''abc'); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB