Performance aspect in loading a different model for some action - php

I just want to know what would be better between below two scenerio.
I have model A and B in CodeIgniter and two different controllers for interacting with those models, That is A_controller and B_controller, Now my question is i have so many of methods defined in both model A and B. Now i want to use a method of model A in A_controller.
It would be better to load whole model A into B_controller to just use a single method, or should i make a separate method in model B for use in B_controller.
Which option will be better with respect to Performance.
Any help will be appreciated!

Divide and conquer
create a small model with the shared functions between models A and B, this way you won't have any duplicated code anywhere. And then you load this new model and the model A for example.
Controller A
$this->load->model('A_model');
$this->load->model('Shared_model');
Controller B
$this->load->model('B_model');
$this->load->model('Shared_model');
Hope that helps

Create one single model file and write functions, so you need to load only one model in controller. All functions can access from a single model.

Nice answer provided by David above. you can also check some good answer for the same
Codeigniter : calling a method of one controller from other
stackoverflow.com/questions/6500022/codeigniter-calling-a-method-of-one-controller-from-other

Related

Should I do data insert for a model inside the controller or inside the model?

As my question title states I would like to know all the best practices and pros/cons of following the "Laravel way" of inserting the data in the db.
Is it a bad practice using the Model::create([...]) for inserting a new row in the database which is related to that controller's model?
Is creating a separate function inside the model itself a much better practice?
If so, why?
It depends on size of your project you can use your models directly in your controller and use the fluent interface of Eloquent to do your logic.
There is nothing wrong that you call a model in a controller to get data from database.
If there is some complicated query you can create method in your model and than call that method in controller ...
In laravel you can use insert in controller , view(blade) or even in route .
But recommended is on your controller. This help to make your code clean
You can take a look for resource controller. It has already predefined logic like store() method where you can put some Model::create(), as you said.
That's not bad if you also create a method inside model and call it from your controller.
I think, that create() method for straght storing some data from request (from some form on front-end, as an example) is enough. But if you want/need to make more than straight storing model, a separate method inside model might take place. Apart to avoid controller's method overgrowth and keep your code pretty.
The answer really depends on, so maybe you will shed some light on the logic you want to implement.

How to create Laravel Model without Eloquent?

As I am not sure, is it possible to create models with DB Class instead of Eloquent? I want to stay away from ORM.
Thanks
Yes of course its possible. You dont need to extend any class to make a model class that encapsulates business logic and consists of methods calling the DB class.
Just create your model inside app/models/MyModel.php like this
class MyModel{
public static function getMyData(){
return DB::table('users')->select('column')->get();
}
}
then you should be fine to call your new class statically:
$data = MyModel::getMyData();
If you wanted to extend the DB class you could, though more likely you would be looking to extend the Database/Builder class to extend functionality but this is a complex topic and I suspect you would have asked a very different question if this was what you were after.
As I final note, I wouldn't steer clear of Eloquent, it's the greatest thing about Laravel amongst a lot of other great things
Just remove the "extends Eloquent" and build the queries using the DB class.

How do I track models using other models?

From time to time one of my models will need to call another. What's the best way to track this?
I was using an array in the base class of each model, so that if Model A called Model B more than once, the second and successive calls would return the existing instance, rather than loading a new one.
While this works fine for simple cases, I can see it has limitations. For example, take the case where Model A loads Model B and Model C; then Model C needs access to Model B (or even A) - it will load the class again!
I suspect that I need a coordinating object, external to the model classes, which acts as a repository. What patterns should I be looking at?
BTW, I am using PHP.
EDIT: Another consideration is that my views may call model methods directly too. In these cases it would make sense to serve up an already instantiated model, rather than a new one...
Thanks!
Take a look at the observer pattern thats used in such case.
Its easy .. each model have a list with listeners and when something happens it says to all Listeners hey something happend

Do I need to create a Model for every Controller? What is the better practise?

Let's say I have a Model 'Retrieve.php' where I have a class named 'Retrieve' and it retrieves posts from a database. Then I have Controller in Index.php where I load that model, retrieve posts and pass it to view.
And now, I have one more page where I have to show those posts. Let's say Sidebar.php or something. And now again, I have to retrieve those posts. So, can I load 'Retrieve.php' once more, or I have to create one more model for Sidebar.php which extends 'Retrieve.php'? What is better practise?
And, in general, do I need for every controller create a new model in a good PHP MVC? If yes, probably Controller and Model should be named the same? Any more advices/comments?
Thank you.
In general, the model should represent a business entity and not a process. I.e., it should be a noun and not a verb. In your case, you want a model for a post, and the methods on that model will perform "the things you do with/to a post." The controller then describes what actions occur for a page. In this case, a controller for the /post page would retrieve a post and pass it to the view for rendering.
Only create the models you need. Remember, the whole point of MVC is that the models are decoupled from the views. This means it's perfectly fine to reuse whatever you need to get the job done. If you have multiple views which need access to the same data, just reuse the same model. Just be sure to give the models descriptive names so there's no confusion as to what they're supposed to represent.
You should only have one Model class for each data structure that that model represents. So if you have 5 Controllers that each access the same Model, you should still only ever have one Model class.
No --
Model should be what your application manages -- so instead of Retrieve, your model class should probably be Post (and maybe you have other Model classes for the nouns in your domain-- Thread, Author...)
The controllers should access the model classes they need to do their jobs; one model class could be used by several controllers, and one controller could use several model classes.

class name collision when using codeigniter 2.0 with Datamapper?

I am using CI2.0 with PHP 5.3
I just started to use “Datamapper ORM” and it is excellent!! however their is a one big problem regarding the classes’ names
I have a database table called “users” so my dm model is “user” and also I have a controller with the same name “user”?
so using the “user” model within the “user” controller is imposible!!
what is the best way to solve this problem?
Many Thanks
best regards
One of the drawbacks in CodeIgniter is that you cannot name a controller, model or library the same thing. This is mainly a PHP issue as obviously you cannot name anything the same, but it can be avoided in two ways.
PHP Namespaces - no can do, they are PHP 5.3 only and 90% of the community would kick off if they were implemented.
Controller Prefixes - This is something I would love to add, but... well it would break stuff for everyone. We'll have to wait until 2.1 at least for a change that big.
For now all I can recommend is you name your models and libraries carefully.
Controller - Users
Library - User
Model - User_model | User_m
It's annoying, but just one of those things for now.
so using the “user” model within the
“user” controller is imposible!!
Umm no it isn't, you need to check the UserGuide more carefully ;)
http://codeigniter.com/user_guide/general/models.html#loading
You may give your model a name other than what it is orginaly defined as:
If you would like your model assigned
to a different object name you can
specify it via the second parameter of
the loading function:
$this->load->model('Model_name', 'fubar');
$this->fubar->function();
All you need to do is change your controller file name and class name to "Users". You don't need to change your model name.
controller: users
model: user
db table: users
Take a look at the Datamapper docs.
I would suggest :
Controller - Users or/and User
Library - Users_lib or Users_library or User_lib or User_library
Model - Users_model or Users_m or User_model or User_m
Keep the words User and Users for the controller so you keep nice url.
Just be consistent with the use of plural or singular for the model and controller.
I named in case of 'Project' like that:
Controller:
Project extends MY_Controller extends CI_Controller
Model:
ProjectModel extends Model
In View
project/projectManager // as main content
project/_projectItem // with _ underscore as subView
That works fine at all.
The way I handle it is to end all my controller name's with _controller.
I use datamapper as well so I had a lot of name collisions. For users I have a User (model) and a User_Controller.
Then i simply add:
$route['user'] = 'user_controller';
to the config/routes.php file and voila.
Only downside of this is that if you have a large project with a lot of pages (like me) you end up with a huge routes.php file especially if you have a lot of functions that have a lot of parameters, the routing can become quite a pain in the ass.

Categories