CodeIgniter Forums
Tutorial (Class 'App\Models\NewsModel' not found) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Tutorial (Class 'App\Models\NewsModel' not found) (/showthread.php?tid=69175)



Tutorial (Class 'App\Models\NewsModel' not found) - Marcos Queiroz - 10-16-2017

Good morning, is anyone already testing CI4 and managed to do the documentation tutorial?

I followed the tutorial at https://bcit-ci.github.io/CodeIgniter4/tutorial/index.html

I'm having trouble trying to use the Model, I'm getting the error that the Model class could not be found.

I'm starting to test this new version of CI, as the way we loaded the Model changed, I wonder if anyone had the problem and solved it.

Thank you very much in advance

[Image: erro_ci4.jpg]

Controller

PHP Code:
<?php

use App\Models\NewsModel;

class 
News extends \CodeIgniter\Controller
{
    public function 
index()
    {
        
$model = new NewsModel(); // --> APPPATH/Controllers\News.php at line 9

        
$data = [
                
'news'  => $model->getNews(),
                
'title' => 'News archive',
        ];

        echo 
view('Templates/Header'$data);
        echo 
view('News/Index'$data);
        echo 
view('Templates/Footer');
    }

    public function 
view($slug null)
    {
        
$model = new NewsModel();

        
$data['news'] = $model->getNews($slug);

        if (empty(
$data['news']))
        {
            throw new \
CodeIgniter\PageNotFoundException('Cannot find the page: '$slug);
        }

        
$data['title'] = $data['news']['title'];

        echo 
view('Templates/Header'$data);
        echo 
view('News/View'$data);
        echo 
view('Templates/Footer');
    }


Model

PHP Code:
<?php
class NewsModel extends \CodeIgniter\Model
{
    protected 
$table 'news';

    public function 
getNews($slug false)
    {
        if (
$slug === false)
        {
            return 
$this->findAll();
        }

        return 
$this->asArray()
        ->
where(['slug' => $slug])
        ->
first();
    }

    public function 
view($slug NULL)
    {
        
$model = new NewsModel();

        
$data['news'] = $model->getNews($slug);

        if (empty(
$data['news']))
        {
            throw new \
CodeIgniter\PageNotFoundException('Cannot find the page: '$slug);
        }

        
$data['title'] = $data['news']['title'];

        echo 
view('Templates/Header'$data);
        echo 
view('News/View'$data);
        echo 
view('Templates/Footer');
    }




RE: Tutorial (Class 'App\Models\NewsModel' not found) - kilishan - 10-16-2017

You didn't namespace your model. Assuming your model is stored at applications/Models/NewsModel.php, you should have this line at the very top of the file:

Code:
namespace App\Models;

That's how the application knows where to find it.


RE: Tutorial (Class 'App\Models\NewsModel' not found) - Marcos Queiroz - 10-16-2017

Thanks, I did not notice.

I would like to congratulate you for the excellent Framework.


RE: Tutorial (Class 'App\Models\NewsModel' not found) - kilishan - 10-16-2017

No problem. And it wasn't in there at the time. I've updated the tutorial though so others should be able to follow along with a little less problems.


RE: Tutorial (Class 'App\Models\NewsModel' not found) - Kim - 12-05-2018

Hi
I just spent an hour before I found this post that helped me.

In the tutorial on:
https://codeigniter4.github.io/CodeIgniter4/tutorial/news_section.html

It would be nice if

PHP Code:
namespace App\Models

is in the code for the model above:

PHP Code:
use CodeIgniter\Model


And CI4 looks very promising Smile
Kim


RE: Tutorial (Class 'App\Models\NewsModel' not found) - ciadmin - 12-05-2018

Fix applied to development version, and is now reflected in the development user guide ... https://codeigniter4.github.io/CodeIgniter4/tutorial/news_section.html


RE: Tutorial (Class 'App\Models\NewsModel' not found) - Jazek - 10-20-2022

I am on very start of learning this grate framework, but have some problems.

I have similar problem, cant understand what is problem, can someone help please?

Model:
Location: app\Models\Main\ToolsModel.php
Code:
<?php
namespace App\Models\Main;
use CodeIgniter\Model;

class ToolsModel extends Model {
  protected $table = 'rent';
  protected $primaryKey = 'id';
  protected $returnType = 'array';
  protected $allowedFields = ['id','title','description','price_day','price_week','image'];

  function getRecords(){
    $query = $this->db->get('rent');
    return $query->result();
  }
}

Controller:
Location: app\Controllers\Main\MainController.php
Code:
<?php
namespace App\Controllers\Main;
use App\Controllers\BaseController;
use App\Models;

class MainController extends BaseController {
  public function index() {

    $db = \Config\Database::connect();
    $rentQuery  = $db->query('SELECT id, title, description, price_day, price_week, image FROM rent');
    $rentTools = $rentQuery->getRowArray();
    $rentTools = model('ToolsModel');
  return view('Main/index', $rentTools);
}

I tryed many ways for namespace and use, nothing works, in controller colored text:
Model in use App\Models; and written Undefined class 'Models'

What am i doing wrong and how to fix?