Welcome Guest, Not a member yet? Register   Sign In
Need suggestion on "Call to a member function save() on null"
#1

Hello There
I am new to codeigniter here.
I am learning codeigniter 4 by following the user_guide provided by codeigniter.com. I followed the lesson page by page and everyting is ok until I reached this section "Create news items

" (https://codeigniter.com/user_guide/tutor...items.html) which is the basic tutorial about how to save data (news item) to database.
I have problem with the example code as shown below
public function create()
{
    $model = model(NewsModel::class);

    if ($this->request->getMethod() === 'post' && $this->validate([
        'title' => 'required|min_length[3]|max_length[255]',
        'body'  => 'required',
    ])) {
        $model->save([
            'title' => $this->request->getPost('title'),
            'slug'  => url_title($this->request->getPost('title'), '-', true),
            'body'  => $this->request->getPost('body'),
        ]);

        echo view('news/success');
    } else {
        echo view('templates/header', ['title' => 'Create a news item']);
        echo view('news/create');
        echo view('templates/footer');
    }
}

After running the controller I got error message which is "Call to a member function save() on null".

any suggestion on this? I copied and pasted the code directly to my editor and follow everything from the tutorial but I got this error.

Thank you very much
Reply
#2

(This post was last modified: 01-13-2022, 08:54 AM by captain-sensible. Edit Reason: added text )

whats your database situation and which one are you using ?

member function is same as class method ; either no data to pass to database or something up with database and its set up . Don't forget the data you pass in for fields has to match field names in database.

A lateral thinking approach might be to populate database, just a manual population of a couple of entries, then see if you can retrieve data using a contoller and pass to view. if that works then try and save.

The less going on the easier to see the mistake . I started with an sqlite database in fact I stuck with it , since WordPress has proved that security of MySQL or Maria is not necessarily more secure than sqlite. IN fact I stuck with sqlite for web. Also you can simply use sqlitebrowser to view /populate data quickly - no phpMyAdmin


A general approach would be to start trimming out the bloat - all that validation and increase output of error . At line 35 in app/Config/Logger.php set it to :

Code:
public $threshold = 9;


hopefully it might be more specific
CMS CI4     I use Arch Linux by the way 

Reply
#3

Does the model exist? Is the namespace correct?
The model() function returns NULL only if the model class is not found.
Reply
#4

You did not created NewsModel . So thats why you are getting that error . Create Modal
Code:
<?php

namespace App\Models;

use CodeIgniter\Model;

class NewsModel extends Model
{
    protected $table = 'news';

    protected $allowedFields = ['title', 'slug', 'body'];
}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB