• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI4 use model's method in view

#1
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 ?
Reply

#2
just call:
PHP Code:
model('CodeigniterModel'); 
Reply

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

#4
(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....
Reply

#5
(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:
PHP Code:
<?= $result_query?>
I would change this world, but God doesn't give me the source.
Reply

#6
(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:
PHP Code:
<?= $result_query?>
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 ...
Reply

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

#8
(06-22-2020, 03:06 AM)InsiteFX Wrote: It is recommended that you do all of this in your controller not the view!
but if i have 100 row how can i do this in controller instead view ?
Reply

#9
Using CodeIgniter Pagination.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#10
(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 ?
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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