Welcome Guest, Not a member yet? Register   Sign In
Calling Model Function
#1

I am very new to CI4 and while I'm getting on well converting the basics of my script based app to CI4, I am strugling with database calls.



From the documentation I believe I configure parameters in my model, and then create functions that utilise these parameters? Something Like this.


PHP Code:
<?php namespace App\Models\Admin;

use 
CodeIgniter\Model;

class 
Admin_nav extends Model {
    
protected 
$table      'event_master';
    protected $primaryKey 'id';

    protected $returnType 'array';
    
    function 
admin_links () {
        
$admin_links $this->orderBy('event_from_date''desc')
                   ->findAll();
    }
    
    function 
score_links ($id) {
        
$score_links $this->where('event_id'$id)
                   ->findAll();
    }



and then call a function from the controller, something like this

PHP Code:
<?php namespace App\Controllers\Admin;

class 
Admin_nav extends BaseController
{
    public function 
index()
    {
        
$event_links model('Admin\Admin_nav');
    
        
$data['links']=$event_links->admin_links();        
        
        
        echo 
view('templates/admin_nav_test',$data);
    }





But am geeting an error "Call to a member function admin_links() on null" which I do not understand?

Please tell me I'm on the right track???
Reply
#2

(This post was last modified: 04-28-2020, 07:02 AM by kilishan.)

You need to return the data from the methods in the model.

PHP Code:
function admin_links () {
    return 
$this->orderBy('event_from_date''desc')
                               ->
findAll();

Reply
#3

Also you need to change this line in your code, it is wrong.

PHP Code:
$event_links model('Admin\Admin_nav');

// should be

$event_links = new model('Admin\Admin_nav'); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(04-28-2020, 08:08 AM)InsiteFX Wrote: Also you need to change this line in your code, it is wrong.

PHP Code:
$event_links model('Admin\Admin_nav');

// should be

$event_links = new model('Admin\Admin_nav'); 

That part are correct. It's a helper function; Please see: https://codeigniter.com/user_guide/models/model.html
Reply
#5

(This post was last modified: 04-29-2020, 02:42 AM by 68thorby68.)

Many thanks. As I said I am converting my script appliaction to CI4 and already the structure and simplicity the framewowrk brings huge benefits.  Not only is extending existing functionality a whole lot easier, but code maintenance and troubleshooting has improved beyond belief.

68thorby68Many thanks. As I said I am converting my script appliaction to CI4 and already the structure and simplicity the framewowrk brings huge benefits.  Not only is extending existing functionality a whole lot easier, but code maintenance and troubleshooting has improved beyond belief.
Just one question though. If I have a dynamic header, how do i call the controller for that dynamic header without repeating the controllers code in every page? i.e. below is typical of the home, about, players, group and subgroup pages:

PHP Code:
public function index()
    {
        
        
        
//SOME CODE HERE
    
        
$data['title'] = $pagename;    
        
        echo 
view('templates/adminHeader'$data);
        echo 
view('admin/'.$pagename$data);
        echo 
view('templates/footer');
    } 

and adminHeader looks like this.

PHP Code:
public function index()
    {
        
$dynamicContent = new \App\Models\Available_pages;
        
        
$data['pages']=$dynamicContent->admin_pages();
        
        echo 
view('templates/admin_header',$data);
    } 

is there a way I can call the adminHeader controller from about, i.e.

PHP Code:
public function index()
    {
        
        
        
//SOME CODE HERE
    
        
$data['title'] = $pagename;    
        
    
        echo 
view($this->load->templates/adminHeader);
    echo 
view('admin/'.$pagename$data);
        echo 
view('templates/footer');
    } 

Many thanks.
Reply
#6

(This post was last modified: 04-30-2020, 09:24 AM by jreklund.)

You place "static" content in your UserController, AdminController or whatever you want to call it. And extend it from your Pages controller. And it will inherit all settings.

PHP Code:
<?php
namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
UserController extends BaseController
{

    
/**
     * An array containg all data sent to the view
     *
     * @var array
     */
    
protected $data = [];

    
/**
     * Constructor.
     */
    
public function __construct()
    {
        
$dynamicContent = new \App\Models\Available_pages;
        
        
$this->data['pages'] = $dynamicContent->admin_pages();
    }
}

//------------------------------------------------------------------------------

namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
Pages extends UserController
{
    
/**
     * Constructor.
     */
    
public function index()
    {
        
//SOME CODE HERE

        
$this->data['title'] = $pagename;
        
        echo 
view('templates/adminHeader'$this->data);
        echo 
view('admin/'.$pagename$this->data);
        echo 
view('templates/footer');
    }

Reply
#7

(This post was last modified: 04-30-2020, 07:39 AM by 68thorby68.)

Many thanks jreklund,

I think I'am getting there, and once I can figure this logic out I will be flying . So to recap if I call my page, that in turn references the dynamic content (page extends dynamic), which in turn references the BaseController (dynamic extends BaseController)?


but what if I want to access more than one controller/view in my page?  i.e. I have a page with dynamic content in the menu (as above), and I totally diiferent dynamic content in the main body of the page, surely I dont have to daisy chain the classes as I've assumed above? (I feel stupid!!).

also, I was getting a 404 Controller method is not found: index. So, changed function __construct () in page controller, to function index().This appers to work, but are there any implications by not using function __construct () in may page controller?



Apologies for being thick, my brain only has a 386 processing capacity.
Reply
#8

I usually only extend one controller and it will load the same content always. That I wan't everywhere.

If you want to use the same content in different controllers you need to convert that code into libraries that you can call/load from everywhere.

I made an error in my code, it should be index().
Reply
#9

Many thanks jreklund,

Looks like my next project is exploring CI4 Libraries.

As a complete newbie to CI4 I must say I am very impressed with the ease of use, documentation and especially the support. I stumbled upon CI4 after working on an Opencart project and deciding to use a MVC framework. Opencart has some really fantastic MVC features, but the documentation is poor and large sections of the online support community is very hostile, and only want to sell their addon or services. Keep up the good work here.

Thanks again.
Reply
#10

It's not much too it, it's a PHP class you place inside app/libraries (or any other folder works). So you don't get any in-depth description of it in the user guide itself.

Here are an example if you are using the core modules strategy.
https://codeigniter.com/user_guide/gener...#libraries

Or if you place it in your app folder.

PHP Code:
// app/Libraries/Widget.php
$widget = new \App\Libraries\Widget(); 

Thanks! That's what we are striving for. Now we need to just put the word out to the rest of the world! I have made two websites with OpenCart, never modified it myself. But the QC on the plugins aren't that great, I mostly bought translations and it where mostly just using Google Translate. :-(
Reply




Theme © iAndrew 2016 - Forum software by © MyBB