-
pippuccio76
Senior Member
-
Posts: 515
Threads: 219
Joined: Jun 2017
Reputation:
2
Hi ,sorry for engish , in ci3 if i have a model call codeigniterModel with a method call findById($id)
in controller i load model with :
PHP Code: $this->load->model('codeigniterModel')
and in view i can use $this-> codeigniterModel->findById($id) method
how can i do the same on ci4 ?
-
Mostafa Khudair
Junior Member
-
Posts: 14
Threads: 5
Joined: Dec 2019
Reputation:
0
just call:
PHP Code: model('CodeigniterModel');
-
Digital_Wolf
Member
-
Posts: 84
Threads: 10
Joined: Mar 2019
Reputation:
4
(06-21-2020, 07:06 AM)pippuccio76 Wrote: Hi ,sorry for engish , in ci3 if i have a model call codeigniterModel with a method call findById($id)
in controller i load model with :
PHP Code: $this->load->model('codeigniterModel')
and in view i can use $this->codeigniterModel->findById($id) method
how can i do the same on ci4 ?
Hello !
You can do it like this:
PHP Code: <?php namespace App\Controllers;
use CodeIgniter\Controller;
// use u model class. use App\Models\codeigniterModel;
class MyController extends Controller { public function index() { // create instance u model. $CI_Model = new codeigniterModel();
// call the method from model echo $CI_Model->findById($id); }
p.s. Sorry my English.
I would change this world, but God doesn't give me the source.
-
pippuccio76
Senior Member
-
Posts: 515
Threads: 219
Joined: Jun 2017
Reputation:
2
(06-21-2020, 10:56 AM)Digital_Wolf Wrote: (06-21-2020, 07:06 AM)pippuccio76 Wrote: Hi ,sorry for engish , in ci3 if i have a model call codeigniterModel with a method call findById($id)
in controller i load model with :
PHP Code: $this->load->model('codeigniterModel')
and in view i can use $this->codeigniterModel->findById($id) method
how can i do the same on ci4 ?
Hello !
You can do it like this:
PHP Code: <?php namespace App\Controllers;
use CodeIgniter\Controller;
// use u model class. use App\Models\codeigniterModel;
class MyController extends Controller { public function index() { // create instance u model. $CI_Model = new codeigniterModel();
// call the method from model echo $CI_Model->findById($id); }
p.s. Sorry my English. I want use in a view not in a controller....
-
Digital_Wolf
Member
-
Posts: 84
Threads: 10
Joined: Mar 2019
Reputation:
4
(06-21-2020, 11:15 AM)pippuccio76 Wrote: (06-21-2020, 10:56 AM)Digital_Wolf Wrote: (06-21-2020, 07:06 AM)pippuccio76 Wrote: Hi ,sorry for engish , in ci3 if i have a model call codeigniterModel with a method call findById($id)
in controller i load model with :
PHP Code: $this->load->model('codeigniterModel')
and in view i can use $this->codeigniterModel->findById($id) method
how can i do the same on ci4 ?
Hello !
You can do it like this:
PHP Code: <?php namespace App\Controllers;
use CodeIgniter\Controller;
// use u model class. use App\Models\codeigniterModel;
class MyController extends Controller { public function index() { // create instance u model. $CI_Model = new codeigniterModel();
// call the method from model echo $CI_Model->findById($id); }
p.s. Sorry my English. I want use in a view not in a controller....
Then you just need to slightly modify the code in the controller itself like this:
PHP Code: ...here everything that I wrote above... // create instance u model. $CI_Model = new codeigniterModel(); // create new data "storage" $data = [ 'result_query' => $CI_Model->findById($id) ]; // passing data to view echo view("your_view_file", $data);
Further, in the view file itself, you need to call the data simply by accessing the key:
I would change this world, but God doesn't give me the source.
-
pippuccio76
Senior Member
-
Posts: 515
Threads: 219
Joined: Jun 2017
Reputation:
2
(06-21-2020, 11:29 AM)Digital_Wolf Wrote: (06-21-2020, 11:15 AM)pippuccio76 Wrote: (06-21-2020, 10:56 AM)Digital_Wolf Wrote: (06-21-2020, 07:06 AM)pippuccio76 Wrote: Hi ,sorry for engish , in ci3 if i have a model call codeigniterModel with a method call findById($id)
in controller i load model with :
PHP Code: $this->load->model('codeigniterModel')
and in view i can use $this->codeigniterModel->findById($id) method
how can i do the same on ci4 ?
Hello !
You can do it like this:
PHP Code: <?php namespace App\Controllers;
use CodeIgniter\Controller;
// use u model class. use App\Models\codeigniterModel;
class MyController extends Controller { public function index() { // create instance u model. $CI_Model = new codeigniterModel();
// call the method from model echo $CI_Model->findById($id); }
p.s. Sorry my English. I want use in a view not in a controller....
Then you just need to slightly modify the code in the controller itself like this:
PHP Code: ...here everything that I wrote above... // create instance u model. $CI_Model = new codeigniterModel(); // create new data "storage" $data = [ 'result_query' => $CI_Model->findById($id) ]; // passing data to view echo view("your_view_file", $data);
Further, in the view file itself, you need to call the data simply by accessing the key:
no,you don't understand , i want use model's functions in view , as ci3 .....
for example : in db table there are foreign key , in the page where i show the list , i cannot write the id of the foreign key but i must insert for example the name ...
-
InsiteFX
Super Moderator
-
Posts: 6,574
Threads: 331
Joined: Oct 2014
Reputation:
240
06-22-2020, 03:06 AM
(This post was last modified: 06-22-2020, 03:06 AM by InsiteFX.)
You would need to do it like this.
PHP Code: <?php model('CodeigniterModel');
$this->codeigniterModel->findById($id); ?>
If you need the id in the view then you can use a hidden form input field.
It is recommended that you do all of this in your controller not the view!
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
-
pippuccio76
Senior Member
-
Posts: 515
Threads: 219
Joined: Jun 2017
Reputation:
2
(06-22-2020, 08:13 AM)InsiteFX Wrote: Using CodeIgniter Pagination. NO , how can i get the culumn of foreign key of 100 record
for example i have i table user:
-name
-surname
-id_club
and a table club
-id
-name
When i show the list of user i don't want show the id_club but i want show the club's name ...
How can i do (without a join) .
In ci3 i can use the model method in view for example (without tag)
table
name surname club
foreach (users as user)
tr
td user->name td
td user->surname td
td this->club_model->findbyid($user->id_club)->name td
tr
how can i do the same in codeigniter 4 ?
|