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.
Related
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
Just really what the title says, does anybody have a decent explanation on how to use models properly in laravel 4? I'm fine with using the pre-existing User model. But I read somewhere that queries and such should be done in a model of your own.
So what I have is basically a form where you can make a status update (like facebook) and it stores it in a database but the query is run through the controller.
I want it to be done through a model.
Any information on this would be great! Thanks in advance
It's a broad question and right place to learn about how to use model in Laravel-4 is the laravel site itself, but anyways.
Actually, model is the the place where you should keep the business logic in any MVC framework, it could be database related or anything else that is the heart of the application and controller and View are just two parts of the application whose responsibility is only communicate to the model, fetch data and present it to the user but all the data processing should be done in the model and controller should be kept slim and view is only a mechanism to present the user a UI.
So, if you want to have a User model in your application (laravel-4) then you may extend it from Eloquent like
class User extends Eloquent{
// code goes here
}
and then call it's methods from the controller, like
$user = User::get(1);
then passing it to the view like
return View::make('viewname', $user);
That's it. But, if you think, where the find method come from then, it's because, you have extended the Eloquent model and in that model all the necessary methods (find e.t.c) are available and your User model inherited the behavior of Eloquent model, so you can use all the methods from User and it's not necessary to a single method in your User model unless you need to write some special methods for that User model, like relationship functions and so. well, the perfect place to seek help or to know the basic about it is the Laravel site, try to understand it from there and if you fail or just stuck anywhere then come here to ask with specific problem. Also, check Basic Database Usage and Query Builder.
BTW, you may check this post for a detailed answer on model of MVC.
I want to access a controller, which is located in application/controller/example.php, from a model - located in application/model/users.php
class Users extends CI_Model {
//access the example.php controller
}
Who could i accomplish this ?
You don't want to ever do that, hence why CodeIgniter cannot do it. You should be accessing everything else from your Controller, not the other way around.
A good old resource from CodeIgniter :)
http://codeigniter.com/user_guide/overview/mvc.html
You dont have to access an controller from a model. Models (Views, helpers, libraries ect) should all be accessed from controllers. ( Model are used from manipulating data ( usually in database ) )
The idea behind the MVC is to have a Model, Viewer and Controller.
In other words the Controller handles the request, the Model gets the data and the Viewer displays something to the user.
The Model is used ONLY to manipulate the data - doesn't matter if it's a database connection or other data resource.
Model is where you store your bussines logic. Controller is where you call things and operate with data. Views are for creating output.
Bassically you can do everything from withing controller, but that is wrong, since it does not gain you any clarity. Try to offload as much as possible to models and views and keep controllers small and readable, well commented.
I have some controllers in my Cake application, namely servers and users. I want to write a simple API and have a controller called ApiController. Within this controller I want to use both the servers and users models.
I'm very new to Cake, but not MVC in general. From what I've picked up so far Cake will automatically use the servers model in the ServersController controller, I don't know how to explicitly use a model from a certain controller.
Also, I want the API requests to only serve JSON without any HTML markup. I have a default layout that defines the header/footer of all my site pages and that gets outputted when I request an API function as well as the JSON from the view. How can I stop the layout from being output and instead just serve the view?
You need to declare the $uses property in your controller see http://book.cakephp.org/2.0/en/controllers.html#controller-attributes
The $uses attribute states which model(s) the will be available to the controller:
<?php
class ApisController extends AppController{
public $uses = array(
'User',
'Server'
);
}
Also you don't appear to be following Cake naming conventions where controller names are plural (Apis or Servers) and model names are singular (Api or Server). These names should also be in CamelCase. See http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html for more information
Regarding the JSON, there is an Ajax layout you can use to help you server JSON requests. See http://book.cakephp.org/2.0/en/views.html#layouts for more information on how to achieve this.
Yottatron's answer is spot on, as is Nick Savage's. It's important to know the differences between the different ways to load a model, which is succinctly covered in following comment: https://stackoverflow.com/a/4753244/117413
Personally, I stay away from overloading the global $uses array since it's very rare that I need a reference to all the model objects at a global level (and it's just bad practice to overload it, as per the Cake docs here: https://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Controllers.html#components-helpers-and-uses )
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.