• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"$this->request->getVar" in a model with CI4

#1
I have tried to use
"$this->request->getVar" in a model.
I didn't succeed.
It only worked when I moved the construct into a controller.
Why? What's my mistake?

Then I tried "$this->db->update($id, $data);" to use in a model.
Error message:
Call to undefined method CodeIgniter \ Database \ MySQLi \ Connection :: update ()
Why?
How do I introduce the method to the model?

With best regards
Ralf
Reply

#2
(01-23-2021, 01:01 PM)divebase Wrote: I have tried to use
"$this->request->getVar" in a model.
I didn't succeed.
It only worked when I moved the construct into a controller.
Why? What's my mistake?

If you need to use Request in a model method, pass a Request instance as a parameter to the method.
Or you can pass to the model constructor.
Or you can contact through the service.
There are many ways.
PHP Code:
$model = new SomeModel();
$model->someMethod($this->request);
//
$model = new SomeModel($this->request);
//
$request App\Config\Service::request(); 


(01-23-2021, 01:01 PM)divebase Wrote: Then I tried "$this->db->update($id, $data);" to use in a model.
Error message:
Call to undefined method CodeIgniter \ Database \ MySQLi \ Connection :: update ()
Why?
How do I introduce the method to the model?

The $db property contains the connection.
To use QueryBuilder call $this->builder()
PHP Code:
$builder $this->builder();
$builder->update(....); 
// or
$this->builder()->update(....); 
Reply

#3
(01-24-2021, 03:06 AM)iRedds Wrote:
(01-23-2021, 01:01 PM)divebase Wrote: I have tried to use
"$this->request->getVar" in a model.
I didn't succeed.
It only worked when I moved the construct into a controller.
Why? What's my mistake?

If you need to use Request in a model method, pass a Request instance as a parameter to the method.
Or you can pass to the model constructor.
Or you can contact through the service.
There are many ways.
PHP Code:
$model = new SomeModel();
$model->someMethod($this->request);
//
$model = new SomeModel($this->request);
//
$request App\Config\Service::request(); 


(01-23-2021, 01:01 PM)divebase Wrote: Then I tried "$this->db->update($id, $data);" to use in a model.
Error message:
Call to undefined method CodeIgniter \ Database \ MySQLi \ Connection :: update ()
Why?
How do I introduce the method to the model?

The $db property contains the connection.
To use QueryBuilder call $this->builder()
PHP Code:
$builder $this->builder();
$builder->update(....); 
// or
$this->builder()->update(....); 
Hi.
Where does the first part placed in?
PHP Code:
$model = new SomeModel();
$model->someMethod($this->request); 
I suppose in the controller or not.

The part:
PHP Code:
$model = new SomeModel($this->request); 
gives me an Error.
'Argument 1 passed to CodeIgniter\Model::__construct() must be an instance of CodeIgniter\Database\ConnectionInterface or null, object given'
Also placed in Controller. Ok?

$request = App\Config\Service::request();
Where have this placed in?

Thanks for help.
Ralf
P.S. I am German. I hope somebody wrote in German too.
Reply

#4
(01-24-2021, 09:06 AM)divebase Wrote: Where does the first part placed in?
PHP Code:
$model = new SomeModel();
$model->someMethod($this->request); 
I suppose in the controller or not.

In the controller, or in the service, or somewhere else where you need to work with the model

(01-24-2021, 09:06 AM)divebase Wrote: The part:
PHP Code:
$model = new SomeModel($this->request); 
gives me an Error.
'Argument 1 passed to CodeIgniter\Model::__construct() must be an instance of CodeIgniter\Database\ConnectionInterface or null, object given'

Of course you will get an error.
You need to override the constructor and call the parent's constructor.

(01-24-2021, 09:06 AM)divebase Wrote: Also placed in Controller. Ok?

In the controller, or in the service, or somewhere else where you need to work with the model

(01-24-2021, 09:06 AM)divebase Wrote: $request = App\Config\Service::request();
Where have this placed in?

In the controller, or in the service, or somewhere else where you need to work with the model. 

This is just an example of getting an instance of the IncomingRequest class.
In the controller, the $this->request property contains the same instance of the class.

Please learn PHP OOP, MVC patterns and Singleton.
This will help you understand how the framework works.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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