Welcome Guest, Not a member yet? Register   Sign In
Tutorial (Class 'App\Models\NewsModel' not found)
#1

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/t...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');
    }

Reply
#2

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

Thanks, I did not notice.

I would like to congratulate you for the excellent Framework.
Reply
#4

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.
Reply
#5

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

In the tutorial on:
https://codeigniter4.github.io/CodeIgnit...ction.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
Reply
#6

Fix applied to development version, and is now reflected in the development user guide ... https://codeigniter4.github.io/CodeIgnit...ction.html
Reply
#7

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




Theme © iAndrew 2016 - Forum software by © MyBB