Welcome Guest, Not a member yet? Register   Sign In
Using model in another model

how can i call model in anothe model in ci4
in ci3 i can use
PHP Code:


PHP Code:
$userModel = new App\Models\UserModel(); 


@kilishan : I looked at the documentation, and I saw nothing about how to load a model. I think it should be one of the first things on the "Using CodeIgniter’s Model" page. I know it is not complicated, but some people might need this information. So, I think I will update the documentation when I have a little time ;-)

@bonaxcrimo : You can call a model like this:

PHP Code:
// Load the Users model
$userModel = new App\Models\UserModel();
// Load the Articles model
$articleModel = new App\Models\ArticleModel(); 

Then you will be able to use it like this:

PHP Code:
// Find a specific user
$specificUser $userModel->find($user_id);
// Get all articles
$allArticles $articleModel->findAll(); 

And you will find more info here: https://codeigniter4.github.io/userguide...model.html

@littlej More docs are always welcome, but I also think it is fair to say that specific pages in the docs assume knowledge of framework general principles. We have a section on namespaces and how things are different from CI3 and develops coming from 3 definitely need to read those before diving in.

(This post was last modified: 01-24-2020, 09:21 AM by PaulD.)


Although I agree with MGatner, that some knowledge has to be assumed, I think littlej is right on this instance IMHO. Many people learning PHP will be using CI, or using a framework for the first time, or just trying CI out for the first time and looking for an expected 'CI' way to load a model. The docs for Models should definitely have that intro info in there, otherwise many people can be left scratching their heads or searching the forum or even worse, giving up with CI at a very early stage.

For instance like CI3: https://codeigniter.com/user_guide/gener...ng-a-model

It also shows how easy CI is to use. A persuasive feature for new users.

Theme © iAndrew 2016 - Forum software by © MyBB