Suppose we have this code:
class My_controller extends CI_Controller
{
private $model_name;
function __construct($model_name)
{
$this->model_name = $model_name;
}
function index()
{
//Use the model here
}
}
How should I create a handle to the model, to be used in the index function?
I mean I don't know the name of the model, so how should I access it?
--EDIT--
Normally, we would be loading a model like this:
$this->load->model('some_model');
$this->some_model->doSomething();
Here, we know the name of the model.
Now, suppose that My_Controller is a class, which some other controllers inherit from. Each class which inherits from My_Controller, set the $model_name property. Here we don't know the name of the model, so we need a handle to that model. How should that handle be retrieved?
I hope this clarifies a bit.
Thanks in advance.
You can give a model an alias.
$this->load->model($this->model_name, '', 'some_model');
$this->some_model->doSomething();
Related
I want to create a base controller class for all my controllers in Symfony, I am very new to Symfony, so don't be angry with dumb question. I am asking this question because I can't do something like this
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class AbstractController extends Controller
{
public function __construct()
{
//...... check access level
$user = $this->getUser(); //This is not working, I don't have access to the Controller(the base class) properties
}
}
Now one of my controllers
class UserController extends AbstractController
{
public deleteUserAction(Request $request)
{
var_dump($this);// this will dump an empty class that is not instance of Symfony\Bundle\FrameworkBundle\Controller\Controller
//.... delete user
}
}
What is the best way to do this? please...
EDIT....
What I really want to do is to check whether a user privilege level is enough to access a particular action(e.g. deleteUserAction()) in a particular controller(e.g. UserController), I have a class that attach privilege level to all actions in all controllers. The check will be very efficient if it happens in a parent controller (e.g. BaseController's constructor) which is executed before UserController->deleteUserAction() but in the base controller I don't have access to $this.
I have tried voter and ACL none help my situation.
Thanks in advance.
I think second one is the best way to create your own class and use common function in it.
If you want to add some common functions of controller then it is not the proper way to add it into the Symfony default controller, Instead you can create BaseController and extend your all the controller with BaseController and your BaseController should extends Controller.
By this way the default controller of the symfony stay untouched.
simply use service controller... is shared:
http://symfony.com/doc/current/controller/service.html
In my CodeIgniter project I am using codeigniter-base-model and extending all my models from there.
In my user model I want to ensure that super users don't get included, unless specified otherwise.
Something like:
$this->db->where('uacc_issuper', 0));
And I suspect I can use the observer $before_get for this. But I'm not sure how go about it.
Any suggestions pointing me in the right direction will be appreciated.
in $before_get you have give array of method name called before any get query, you need to add those methods in your child model
class page_model extends MY_Model{
protected $before_get = array('method_name');
function __construct(){
parent::__construct();
}
function method_name(){
$this->db->where('uacc_issuper', 0));
}
}
now if you try to get anything with page model this method will be called every time
$page = $this->page->get(1);
Coming from C#/.NET MVC 3/4, I'm not really used to CodeIgniter's implementation of models.
The documentation shows models being loaded within controller methods, however I'm using the model in almost every method and my model is storing data used across its methods in properties via its constructor.
Is there any reason NOT to instantiate the model in the controller constructor that I'm overlooking?
You can load model as per following ways also :
means if you have your model in any folder so using following code you can load model in controller.
$this->load->model('modelFolder/' . $this->controller . '_model');
For eg. :
if you have your model in folder named "modelFolder" then do like this :
class demoController extends CI_Controller {
var $controller = "user";
/* Local Constructor Will Be Overriding The One In The Parent Controller Class So We Need To Manually Call It. */
public function __construct() {
parent::__construct();
$this->load->model('modelFolder/' . $this->controller . '_model');
$this->load->model('common_model');
}
}
Hope it will help you...
There is no reason not to load the model for every controller activation. It could even be put in the configuration's autoload for all controllers.
The only reason not to always load it would be if many operations do not need the model. Then you could save a little bit of memory and time.
Heres what im trying to do
Ive got table that holds many different types of data for items. the model name for this is 'Object'
for example :
row 1 : item_type = event
row 2 : item_type = news
row 3 : item_type = booking
i want to write a controller 'event_controller' and use the 'Object' model with it and only deal with item_types of event. and another controller for news (news_controller) and use the same model.
how do i do this on cakephp.
Im coming into cake from codeigniter and in CI we can load any model we want into any controller can i do something similar with cake?
It's considered poor form to use var $uses in CakePHP 1.3+. It's been replaced by App::import (see below)
Given you have a Users controller with User model, and a Comments controller with Comment model. The accepted patterns are:
Using Associations
This is your best bet if the models can be logically associated.
// models/user.php
Class User extends AppModel {
public $hasMany = array('Comment');
}
// controllers/users_controller.php
Class UsersController extends AppController {
function allComments() {
$this->User->Comment->find('all'); // You can use this across multiple models (e.g. $this->User->Comment->NestedComment->find('all');
}
}
Instantiating the model object
This will load your model file, add the instance to CakePHP's object map, and returns the instance.
// models/user.php
Class User extends AppModel {}
// models/comment.php
Class Comment extends AppModel {}
// controllers/users_controller.php
Class UsersController extends AppController {
function allComments() {
$Comments =& ClassRegistry::init('Comment');
$Comments->find('all');
}
}
Using $this->loadModel
Internally this uses ClassRegistry::init, then also adds the model as a property of the controller.
// models/user.php
Class User extends AppModel {}
// models/comment.php
Class Comment extends AppModel {}
// controllers/users_controller.php
Class UsersController extends AppController {
function allComments() {
$this->loadModel('Comment');
$this->Comment->find('all'); // using load model allows you to access it via $this->Model
}
}
App::import
This is really just the CakePHP way of requiring a file. You'll still need to instantiate the object.
// models/user.php
Class User extends AppModel {}
// models/comment.php
Class Comment extends AppModel {}
// controllers/users_controller.php
App::import('Model', 'Comment');
Class UsersController extends AppController {
function allComments() {
$Comment = new Comment();
$Comment->find('all');
}
}
I hope this helps.
Edit: If you want to use the model object globally within the controller, you can use any of these patterns I specified within your beforeFilter().
I would like to suggest you to not to $uses statement. Instead of that you can use relations of the models like $this->Model->M0del1->....->someFunc(); if the relation exists.
If the relation between the models dos't exist then simply use $this->loadModel('ModelName'); in the specific function where-ever you need it.
If you use var $uses = array('Object'); it becomes global to the controller and it will load that model for all the actions of the controller despite of whether you require it or not. This will affect your performance.
If you use $this-LoadModel('ModelName'); in a particular function it will only load in that function not in all the actions.
You declare your controller and declare the $uses variable on it like this:
var $uses = array('Object');
Hopefully a simple question: I've a plugin which uses a set of tables (kb_items, kb_item_tags, etc). and I'd like to be able to access these models from another controller (say, my Pages controller), thus:
class PagesController extends AppController{
function knowledgebase(){
$items = $this->KbItem->findAll(...);
}
}
I am admittedly breaking the rules a little (by not placing this controller inside the knowledge base plugin), but this in this case its a custom page that doesn't need to be part of the knowledge base plugin code base.
Please let me know if you need more details. Thanks in advance for any help!
I just had to do this myself, and putting the model name in the 'Uses' array does work. If you don't need to access the model in multiple controller actions, you can also use loadModel() to access it in just the actions you need. For example, let's say you only need to access this model in the view() action of a given controller:
function view() {
// load the model, making sure to add the plug-in name before the model name
// I'm presuming here that the model name is just 'Item', and your plug-in is called 'Kb'
$this->loadModel('Kb.Item');
// now we can use the model like we normally would, just calling it 'Item'
$results = $this->Item->find('all');
}
Hope that helps.
Not sure if it works like this in 1.1 but in 1.2+ you prefix the model name with the plugin name and a period in the controller's uses array:
class PagesController extends AppController
{
var $uses = array('Page','Kb.KbItem');
function knowledgebase()
{
// This now works
$items = $this->KbItem->findAll();
}
}
Just add the models to your controllers' $uses property:
class PagesController extends AppController
{
var $uses = array('Page','KbItem');
function knowledgebase()
{
// This now works
$items = $this->KbItem->findAll();
}
}