Codeigniter: how to use multiple 'MY_Model' in my project? - php

I am new to CI and I am maintaining one project build in CI, previously developed by another guy.
This guy create his own MY_Model in application/core and all models he created extend that MY_Model.
I don't like the core model built by him and want to use Jamie Rumbelow's MY_Model instead in the other models I will create recently.
But since almost all the old models extended the MY_Model already, I can't just replace MY_Model.
How can I use Jamie Rumbelow's MY_Model' and still make old models work?
Thanks for any advices!

You'd have to rename one of the MY_Model classes and put both of them in the MY_Model.php file (or include the renamed one from there). For example, if you were to rename Jamie Rumbelow's class to JR_Model instead, here's how your MY_Model.php file would look like:
class MY_Model extends CI_Model {
// your predecessor's code
}
class JR_Model extends CI_Model {
// Jamie Rumbelow's code
}
... then you can extend from JR_Model wherever you like, without breaking your old models.
Note that it's important to keep the filename as 'MY_Model.php', because that's what CodeIgniter will look for when loading a CI_Model extension.

Related

Extending own class in PHP CodeIgniter 3

I have two models: models/Categories.php and models/backend/BE_categories.php
I want BE_categories class to extend Categories, since it has some useful functions I can re-use:
Categories.php
class Categories extends CI_Model
{
...
BE_categories.php
class Be_categories extends Categories
{
...
But when $this->load->model('models/be_categories') I get the error: Unable to locate the specified class: Session.php yet it works with exending CI_Model What have I done wrong?
Just my guess, you load Session library inside some function in Categories model,and then try to use function with session library specific function inside model BE_Categories.
Add Session library in config/autoload.php or load it inside Categories model constructor, and remember to add constructor inside BE_Categories.
I also had a Controller called Categories.php, with class Categories, which is where CI was getting confused.
I had to rename models/Categories.php to models/Categories_model.php as well as the class name.
But also had to include the file when extending:
BE_categories_model.php
include('application/models/Categories_model.php');
class Be_categories_model extends Categories_model
{
...
Edit:
Thanks to commenter, might be better to to include this model in config/autoload.php instead of using PHP's include() function.

Common functions in CodeIgniter

I am new to CodeIgniter.
I am using HMVC in CodeIgniter and want to use a module function in many other modules:
e.g I have a Locaton_model with function get_locations($param) { return; }
How do I use the above function in many other modules? Should I load the model in other module controllers every time I need this function or define the function some where globally?
You can easily achieve that by using core controllers:
http://ellislab.com/codeigniter/user-guide/general/core_classes.html
Instead of beginning your model with:
class Some_model extends CI_Model {}
You start with:
class Some_model extends MY_Model {}
Edit:
It is also possible to use libraries:
http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
This is useful when you want more general things, like a search engine, a IMAP interface, that kind of stuff.
if your creating your own model first make sure it is inside the core folder second on your controller extends the model name like this
class myController extends Locaton_model
{
function index()
{
$this->load->model->("your model name");
$this->yourmodelname->functionname($param);
}
}

Extending all controller classes from my custom controller class in symfony2

Like in Codeigniter we do have 'core' folder where we can define our own controller like 'MY_Controller' and can be used to extend all the class to extend from this controller is there any possibility to do so in Symfony2.
In symfony I want to create class 'MY_Controller' which extends from the base class 'Controller', and I want all the classes in the controllers to extend from MY_Controller' class.
Thanks in Advance...
Note:
When working with Symfony2 it is strongly recommended you follow the Symfony2 coding style. It's basically the same as PHP-FIG, with one or two deviations. So underscores are a no-no in class names. Other than that: Symfony is pretty easy to work with, and fully OO, so changing the class a controller extends from is as simple as replacing extends Controller with extends AnotherClass.
But now, the symfony2-way of using a custom controller:
What you could do, is create a Core bundle (CoreBundle henceforth). Then, in this CoreBundle, define a controller, that extends from the Symfony Controller component. From the command line, in your project root, use this command:
php app/console generate:bundle --namespace=YourNameSpace/CoreBundle --bundle-name=YourNameSpaceCoreBundle
More options can be found here
After that, you'll find a DefaultController class in the bundle directories. (probably in the folder src/YourNamespace/CoreBundle/Controller). Then, set about generating your Core controller:
php app/console generate:controller --controller=YourNameSpaceCoreBundle:Core
See the documentation for more options on how to generate your core controller.
After you've finished setting up your custom controller, you can use it in any of the other bundles at will:
namespace YourNameSpace\AnotherBundle\Controller;
use YourNameSpace\CoreBundle\Controller\CoreController;
class DefaultController extends CoreController
{//extends from your custom controller
}
And that's it: you're done.
First, don't name the class using underscores as in PSR-0 each underscore char is converted to a directory separator, when used in class name.
Second, put your controllers to <bundledir>/Controller/
Third, name your controller something like BaseController and extend all other controllers from it.
Fourth, think of using dependency injection rather than coupling functionality in a base controller.

Creating a helper or duplicating functions in codeigniter

I have a set of functions that are called from 2 different models [ and maybe more in the future ]
What's the best approach to deal with this :
1 - Duplicated in each model
2 - Creating a helper for those functions and loading that helper from each model
Do you suggest any other approach
You can create a base model that your individual models extend, giving them all a shared ancestor of sorts.
Create the file application/core/MY_Model.php
class MY_Model extends CI_Model {
public function common_method($param)
{
// Stuff goes here
}
}
Then, any model that you wish to use the common method(s) in should simply extend MY_Model instead of CI_Model.
Note that the MY_ prefix is the default for CI, but you can change it in the application/config/config.php file.
Its my approach i don't know its a proper one or not.
If you are using this function throughout the site, You can create the class with those functions and add it in a library.
If you like you can do autoload also if require. Based on your usage.

Inheritance in CodeIgniter

I am building a series of forms, and I am trying to inherit the functionality of a parent Form class into all the forms. For example,
LeaveForm extends Form (Model)
LeaveFormController extends FormController
I am handling all the leave form specific stuff in LeaveFormController and LeaveForm.
In LeaveFormController constructor, I simply call the parent class constructor, then load the LeaveForm Model. And in FormController constructor, I load Form model.
My problem is, I get an error,
Cannot redeclare class form in Form.php
Have I got my architecture wrong? How do I handle this ?
check if the class has already been initialized like this:
if (!class_exists('classname'))
{
// ok fine create new instance now
}
Possibly when you $this->load->model('Form'), you manually included the models/form.php file?
In your leaveform.php model file, make sure you load the superclass model you extend using codeigniter's model loading mechanism instead of require or include. Codeigniter has a loader that keeps track of already-loaded files to avoid redeclaring classes, but you need to use $this->load to use it. It won't know about files loaded directly with include or require.
So at the top of leaveform.php, use this:
$CI =& get_instance(); $CI->load->model('Form');
This is not related, but you will have pain unless you namespace your CodeIgniter model classes the same way you namespace Controller classes.
Try using FormModel extends CI_Model {}; Instead of Form extends CI_Model {};

Categories